如何创建带有差异标签的多线图

时间:2019-01-10 07:54:45

标签: charts chart.js line multiline

我需要创建一个图表,其中包含来自不同站点的不同价格,并且不能同时进行。实际上,我正在使用javascript库chart.js。我想知道在同一日期是否可以有不同的点数吗?

    "{
      "site1":[
         {
           "datetime":"2019-01-09 14:43:58",
           "price":"649.99"
         },
         {
           "datetime":"2019-01-09 14:44:17",
           "price":"649.99"
         },
         {
           "datetime":"2019-01-09 15:02:59",
           "price":"649.99"
          },
          {
            "datetime":"2019-01-09 15:05:43",
            "price":"649.99"
          },
          {
            "datetime":"2019-01-09 15:08:52",
            "price":"649.99"
          },
          {
            "datetime":"2019-01-09 15:16:51",
            "price":"649.99"
          }
       ],
       "site2":[
         {
           "datetime":"2019-01-09 15:03:05",
           "price":"0"
         },
         {
           "datetime":"2019-01-09 15:05:52",
           "price":"804.91"
         },
         {
           "datetime":"2019-01-09 15:09:00",
           "price":"804.91"
         },
         {
           "datetime":"2019-01-09 15:16:58",
           "price":"804.91"
         }
       ]
     }"

这是图形的数据示例。

很抱歉,我的解释不简单。

谢谢

2 个答案:

答案 0 :(得分:1)

当您根据时间绘制图表时,可以使用Chart.js + Moment.js来实现。

请参见下面的代码或小提琴-> http://jsfiddle.net/goz3jehy/4/

var config = {
  type: 'line',
  data: {
    datasets: [{
        label: "site1",
        backgroundColor: 'red',
        borderColor: 'pink',
        fill: false,
        data: [{
            x: '2019-01-09 14:43:58',
            y: 649.99
          },
          {
            x: '2019-01-09 14:44:17',
            y: 649.99
          },
          {
            x: "2019-01-09 15:02:59",
            y: 649.99
          },
          {
            x: "2019-01-09 15:05:43",
            y: 649.99
          },
          {
            x: "2019-01-09 15:08:52",
            y: 649.99
          },
          {
            x: "2019-01-09 15:16:51",
            y: 700
          }
        ],
      },
      {
        label: "site2",
        backgroundColor: 'orange',
        borderColor: 'yellow',
        fill: false,
        data: [{
            x: "2019-01-09 15:03:05",
            y: 0
          },
          {
            x: "2019-01-09 15:05:52",
            y: 804.91
          },
          {
            x: "2019-01-09 15:09:00",
            y: 804.91
          },
          {
            x: "2019-01-09 15:16:58",
            y: 804.91
          }
        ],
      }
    ]
  },
  options: {
    scales: {
      xAxes: [{
        type: 'time',
        time: {          
        }
      }],
    },
  }
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);

答案 1 :(得分:1)

是的,可以通过使用time axis来实现。您将需要重新构建数据以适合所需的输入格式。下面是使用forEach的代码段中的一个示例。

请务必注意,Chart.js需要Moment.js进行日期时间处理。确保在Chart.js之前包含Moment.js,或使用Chart.js bundled build

let j = {
    "site1": [{
        "datetime": "2019-01-09 14:43:58",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 14:44:17",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 15:02:59",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 15:05:43",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 15:08:52",
        "price": "649.99"
      },
      {
        "datetime": "2019-01-09 15:16:51",
        "price": "649.99"
      }
    ],
    "site2": [{
        "datetime": "2019-01-09 15:03:05",
        "price": "0"
      },
      {
        "datetime": "2019-01-09 15:05:52",
        "price": "804.91"
      },
      {
        "datetime": "2019-01-09 15:09:00",
        "price": "804.91"
      },
      {
        "datetime": "2019-01-09 15:16:58",
        "price": "804.91"
      }
    ]
  },
  k = Object.keys(j),
  datasets = [];

// remodel the data so it can be provided direct to Chart.js
k.forEach(function(val) {
  let d = {
    label: val,
    data: []
  };

  j[val].forEach(function(val2) {
    d.data.push({
      x: val2.datetime,
      y: val2.price
    });
  });

  datasets.push(d);
});

// create the chart.
new Chart(document.getElementById('canvas'), {
  type: 'line',
  data: {
    datasets: datasets
  },
  options: {
    scales: {
      xAxes: [{
        type: 'time'
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>