高图中无关的图形线

时间:2019-05-06 10:03:54

标签: javascript highcharts

我使用高图制作了温度和时间图表,但是当我单击1周,3天或1天的范围时。图表在某些点上变得凌乱,并在图表中绘制了不相关的图形线,如下图所示。

您还可以在jsfiddl e Demo 上对其进行检查。

您可以在此链接上找到图表的数据 Data

Image

代码

<!DOCTYPE html>
<html>
<head>
    <title></title>
     <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/highcharts-more.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://code.highcharts.com/stock/modules/export-data.js"></script>
<style>
@import 'https://code.highcharts.com/css/highcharts.css';

#container {
    height: 400px;
    max-width: 800px;
    margin: 0 auto;
}
.highcharts-xaxis-grid .highcharts-grid-line {
    stroke-width: 2px;
    stroke: #d8d8d8;
}
.highcharts-xaxis .highcharts-tick {
    stroke-width: 2px;
    stroke: #d8d8d8;
}
.highcharts-minor-grid-line {
    stroke-dasharray: 2, 2;
}
</style>
</head>
<body>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>



</body>
</html>
<script type="text/javascript">
// Notice that the dataset has missing data
$.getJSON('https://api.myjson.com/bins/m5imk', function (data) {

  Highcharts.stockChart('container', {

    chart: {
      type: 'spline'
    },
    rangeSelector: { enabled: false },
                scrollbar: { enabled: false },
     xAxis: {
         gridLineColor: '#f44242',
        minorTickInterval: 'auto',
        startOnTick: true,
        endOnTick: true
    },
    yAxis: {
        gridLineColor: '#f44242'
    },
    rangeSelector: {
      buttons: [
      {
        type: 'day',
        count: 1,
        text: '1d'
      },
      {
        type: 'day',
        count: 3,
        text: '3d'
      }, {
        type: 'week',
        count: 1,
        text: '1w'
      }, {
        type: 'month',
        count: 1,
        text: '1m'
      }, {
        type: 'month',
        count: 6,
        text: '6m'
      }, {
        type: 'year',
        count: 1,
        text: '1y'
      }, {
        type: 'all',
        text: 'All'
      }],
      selected: 3
    },

    title: {
      text: 'Temperature variation by day'
    },

    tooltip: {
      valueSuffix: '°C',
      valueDecimals: 1,
    },

    series: [{
      name: 'Temperatures',
      data: data,
      color: '#BF0B23',

        marker: 
        {
            fillColor: 'blue',
            lineWidth: 0

        }
    }]

  });
});



</script>

1 个答案:

答案 0 :(得分:3)

您在控制台中有Highcharts Error #15,这意味着您有未排序的数据:https://www.highcharts.com/errors/15/

在创建图表之前,您需要对数据进行排序:

data.sort(function(a, b) {
    return a[0] - b[0]
});

实时演示: https://jsfiddle.net/BlackLabel/wtc0pfu8/