【问题标题】:ZingChart X-axis labels do not evenly spaceZingChart X 轴标签的间距不均匀
【发布时间】:2020-03-25 19:56:45
【问题描述】:

我试图让 X 轴(时间)缩放保持一致。根据我输入图表的范围(可能是 1 小时,也可能是 11 小时),间距不会保持一致。如下例所示,在两小时内,您会在 2:30 标记处看到中心标签,但出于某种原因,ZingChart 选择 2:40 作为中间标签。
我尝试过使用“max-labels”属性以及自动调整,但没有运气。

当前代码:

window.addEventListener('load', function() 
{
    //zingchart.EXPORTURL = 'http://naskep/zing/export/index.php';
    zingchart.render(
    {
    id: "myChart",
    width: "100%",
    height: "700",
    data:       {
                type: 'line',
                backgroundColor: '#FFFFFF',
                title:      {
                            text: "Historical Trending Demo"
                            },
                plotarea:   {
                            margin: '50 100'
                            },
                plot:       {
                            marker: {size: '1'},
                            preview:    {
                                        type: "area", //"area" (default) or "line"
                                        'line-color': "#0099ff",
                                        'line-width': 2,
                                        'line-style': "dotted",
                                        'background-color': "#99cc00",
                                        alpha: 1,
                                        'alpha-area': 0.1
                                        }
                            },
                scaleX:     {
                            //labels: myLabels,
                            autoFit: false,
                            transform: {
                                        type: "date",
                                        all: "%m/%d/%y %g:%i:%s %a"
                                        },
                            'max-labels': 6,
                            step: "minute",
                            'max-value': <?php echo $endtime; ?>,
                            zooming: true,
                            },
                scaleY:     {
                            zooming: false,
                            minorGuide: {
                                        lineColor: '#7F8C8D',
                                        lineWidth: 3
                                        },
                            minorTick:  {
                                        lineColor: '#7F8C8D'
                                        },
                            minorTicks: 1,
                            tick:       {
                                        lineColor: '#7F8C8D',
                                        lineWidth: 1
                                        }
                            },
                crosshairX: {
                            marker:     {
                                        alpha: 0.5,
                                        size: '7px'
                                        },
                            plotLabel:  {
                                        borderRadius: '3px',
                                        multiple: true
                                        },
                            scaleLabel: {
                                        backgroundColor: '#53535e',
                                        borderRadius: '3px'
                                        }
                            },
                preview:    {
                            adjustLayout: true,
                            borderColor: '#000000',
                            backgroundColor: '#EAEDED',
                            label:  {
                                    fontColor: '#CD5C5C'
                                    },
                            live: false,
                            mask:   {
                                    backgroundColor: '#7F8C8D'
                                    }
                            },
                series:     [{
                            values: myData
                            }]
                }
    })
}
);

</script>```

【问题讨论】:

    标签: javascript zingchart


    【解决方案1】:

    通常,日期时间刻度项是自动间隔的,因此设置maxItems 往往会弄乱逻辑。不过如果真的需要使用maxItems,也请加

    normalize : false
    

    在它旁边(在scaleX 对象中)。另外,如果您可以接受,为了能够压缩更多标签,您可以尝试"%m/%d/%y&lt;br&gt;%g:%i:%s %a" 格式。

    // window.onload event for Javascript to run after HTML
    // because this Javascript is injected into the document head
    window.addEventListener('load', () => {
      // Javascript code to execute after DOM content
    
      // full ZingChart schema can be found here:
      // https://www.zingchart.com/docs/api/json-configuration/
      let chartConfig = {
    
      "graphset": [
    
        {
    
          "type": "line",
    
          "backgroundColor": "#FFFFFF",
    
          "title": {
    
            "text": "Outside Temp"
    
          },
    
          "plotarea": {
    
            "margin": "50 100"
    
          },
    
          "plot": {
    
            "marker": {
    
              "size": "1"
    
            },
    
            "preview": {
    
              "type": "area",
    
              "line-color": "#0099ff",
    
              "line-width": 2,
    
              "line-style": "dotted",
    
              "background-color": "#99cc00",
    
              "alpha": 1,
    
              "alpha-area": 0.1
    
            }
    
          },
    
          "scaleX": {
            normalize: false,
            "autoFit": false,
    
            "label": {
    
              "text": "Date/Time"
    
            },
    
            "transform": {
    
              "type": "date",
    
              "all": "%m/%d/%y %g:%i:%s %a"
    
            },
    
            maxItems: 3,
            itemsOverlap: true,
    
            "step": "minute",
    
            "max-value": 1585238400000,
    
            "zooming": true
    
          },
    
          "scaleY": {
    
            "label": {
    
              "text": "°F"
    
            },
    
            "minorGuide": {
    
              "lineColor": "#7F8C8D",
    
              "lineWidth": 3
    
            },
    
            "minorTick": {
    
              "lineColor": "#7F8C8D"
    
            },
    
            "minorTicks": 1,
    
            "tick": {
    
              "lineColor": "#7F8C8D",
    
              "lineWidth": 1
    
            },
    
            "zooming": false
    
          },
    
          "crosshairX": {
    
            "marker": {
    
              "alpha": 0.5,
    
              "size": "7px"
    
            },
    
            "plotLabel": {
    
              "borderRadius": "3px",
    
              "multiple": true
    
            },
    
            "scaleLabel": {
    
              "backgroundColor": "#53535e",
    
              "borderRadius": "3px"
    
            }
    
          },
    
          "preview": {
    
            "adjustLayout": true,
    
            "borderColor": "#000000",
    
            "backgroundColor": "#EAEDED",
    
            "label": {
    
              "fontColor": "#CD5C5C"
    
            },
    
            "live": false,
    
            "mask": {
    
              "backgroundColor": "#7F8C8D"
    
            }
    
          },
    
          "series": [
    
            {
    
              "values": [
                [1585227652204, 38],
    
                [1585227712217, 38],
    
                [1585227772231, 38],
    
                [1585227832245, 38],
    
                [1585227892259, 38],
    
                [1585227952271, 38],
    
                [1585228012285, 38],
    
                [1585228072299, 38],
    
                [1585228132312, 38],
    
                [1585228192327, 38],
    
                [1585228252340, 39],
    
                [1585228312353, 39],
    
                [1585228372366, 39],
    
                [1585228492394, 39],
    
                [1585228552408, 39],
    
                [1585228612421, 39],
    
                [1585228672435, 39],
    
                [1585228732448, 39],
    
                [1585228792462, 39],
    
                [1585228852476, 39],
    
                [1585228912490, 39],
    
                [1585228972504, 39],
    
                [1585229032516, 39],
    
                [1585229092529, 40],
    
                [1585229152544, 40],
    
                [1585229212556, 40],
    
                [1585229272571, 40],
    
                [1585229332589, 40],
    
                [1585229392599, 40],
    
                [1585229452611, 40],
    
                [1585229512624, 40],
    
                [1585229572638, 40],
    
                [1585229632652, 40],
    
                [1585229692666, 40],
    
                [1585229752680, 40],
    
                [1585229812693, 40],
    
                [1585229872707, 40],
    
                [1585229932722, 40],
    
                [1585229992734, 41],
    
                [1585230052747, 41],
    
                [1585230112761, 41],
    
                [1585230172775, 41],
    
                [1585230232789, 41],
    
                [1585230292801, 41],
    
                [1585230352814, 41],
    
                [1585230412828, 41],
    
                [1585230472843, 41],
    
                [1585230532856, 41],
    
                [1585230592869, 41],
    
                [1585230652883, 41],
    
                [1585230712899, 41],
    
                [1585230772910, 41],
    
                [1585230832924, 41],
    
                [1585230892938, 41],
    
                [1585230952951, 42],
    
                [1585231012969, 42],
    
                [1585231072979, 42],
    
                [1585231132992, 42],
    
                [1585231193011, 42],
    
                [1585231253019, 42],
    
                [1585231313033, 42],
    
                [1585231373046, 42],
    
                [1585231433060, 42],
    
                [1585231493073, 42],
    
                [1585231553087, 42],
    
                [1585231613101, 42],
    
                [1585231673114, 42],
    
                [1585231733127, 42],
    
                [1585231793142, 42],
    
                [1585231853155, 43],
    
                [1585231913169, 43],
    
                [1585231973181, 43],
    
                [1585232033196, 43],
    
                [1585232093209, 43],
    
                [1585232153222, 43],
    
                [1585232213237, 43],
    
                [1585232273249, 43],
    
                [1585232333264, 43],
    
                [1585232393276, 43],
    
                [1585232453292, 43],
    
                [1585232513305, 43],
    
                [1585232573317, 43],
    
                [1585232633332, 43],
    
                [1585232693345, 44],
    
                [1585232753359, 44],
    
                [1585232813372, 44],
    
                [1585232873386, 44],
    
                [1585232933400, 44],
    
                [1585232993414, 44],
    
                [1585233053426, 44],
    
                [1585233113440, 44],
    
                [1585233233467, 44],
    
                [1585233293482, 44],
    
                [1585233353494, 44],
    
                [1585233413509, 44],
    
                [1585233473522, 44],
    
                [1585233533535, 44],
    
                [1585233593548, 44],
    
                [1585233653562, 45],
    
                [1585233713576, 45],
    
                [1585233773591, 45],
    
                [1585233833604, 45],
    
                [1585233893617, 45],
    
                [1585233953631, 45],
    
                [1585234013644, 45],
    
                [1585234073658, 45],
    
                [1585234133671, 45],
    
                [1585234193685, 45],
    
                [1585234253698, 45],
    
                [1585234313714, 45],
    
                [1585234373724, 45],
    
                [1585234433740, 45],
    
                [1585234493752, 46],
    
                [1585234553766, 46],
    
                [1585234613779, 46],
    
                [1585234673793, 46],
    
                [1585234733806, 46],
    
                [1585234793820, 46],
    
                [1585234853834, 46],
    
                [1585234913847, 46],
    
                [1585234973862, 46],
    
                [1585235033874, 46],
    
                [1585235093889, 46],
    
                [1585235153902, 46],
    
                [1585235213917, 46],
    
                [1585235273929, 46],
    
                [1585235333943, 46],
    
                [1585235393956, 47],
    
                [1585235453969, 47],
    
                [1585235513984, 47],
    
                [1585235573998, 47],
    
                [1585235634011, 47],
    
                [1585235694024, 47],
    
                [1585235754037, 47],
    
                [1585235814051, 47],
    
                [1585235874065, 47],
    
                [1585235934079, 47],
    
                [1585235994092, 47],
    
                [1585236054106, 47],
    
                [1585236114120, 47],
    
                [1585236174132, 47],
    
                [1585236234147, 47],
    
                [1585236294159, 47],
    
                [1585236354173, 47],
    
                [1585236414187, 47],
    
                [1585236474202, 47],
    
                [1585236534214, 47],
    
                [1585236594228, 47],
    
                [1585236654241, 47],
    
                [1585236714254, 47],
    
                [1585236774270, 47],
    
                [1585236834283, 47],
    
                [1585236894297, 47],
    
                [1585236954309, 47],
    
                [1585237014325, 47],
    
                [1585237074336, 47],
    
                [1585237134349, 47],
    
                [1585237194365, 47],
    
                [1585237254378, 47],
    
                [1585237314391, 47],
    
                [1585237374404, 47],
    
                [1585237434419, 47],
    
                [1585237494432, 47],
    
                [1585237554446, 47],
    
                [1585237614460, 47],
    
                [1585237674472, 47],
    
                [1585237734487, 47],
    
                [1585237794499, 47],
    
                [1585237854513, 47],
    
                [1585237914527, 47],
    
                [1585237974541, 47],
    
                [1585238034555, 47],
    
                [1585238094568, 47],
    
                [1585238154581, 48],
    
                [1585238214595, 48],
    
                [1585238274608, 48],
    
                [1585238334623, 48],
    
                [1585238394636, 48]
              ]
    
            }
    
          ]
    
        }
    
      ]
    
    };
    
      // render chart
      zingchart.render({
        id: 'myChart',
        data: chartConfig,
        height: '100%',
        width: '100%',
      });
    });
    .zc-html, .zc-body {
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
    }
    
    .chart--container {
      height: 100%;
      width: 100%;
      min-height: 530px;
    }
    
    .zc-ref {
      display: none;
    }
    <!DOCTYPE html>
    <html class="zc-html">
    	<head>
        <meta charset="utf-8">
        <title>ZingSoft Demo</title>
    		<script src="https://cdn.zingchart.com/zingchart.min.js"></script>
      </head>
      <body class="zc-body">
        <!-- CHART CONTAINER -->
        <div id="myChart" class="chart--container">
          <a class="zc-ref" href="https://www.zingchart.com/">Powered by ZingChart</a>
        </div>
      </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 1970-01-01
      相关资源
      最近更新 更多