【问题标题】:How do I create a CanvasJS Chart using JSON data?如何使用 JSON 数据创建 CanvasJS 图表?
【发布时间】:2017-06-27 03:34:55
【问题描述】:

我创建了一个测试图表,它工作正常。我想通过 ajax 获取我的数据,而不是嵌入到网页中。我认为我需要格式化我的 dataPoints 数组 ({ x: new Date(2009, 0), y: 15 },),以便它们可以在外部 JSON 文件中使用。在我的示例中,填充图表的数据格式为:

My JSChart Fiddle Example

{
  name: "File Sharing",
  showInLegend: true,
  type: "stackedColumn100",
  color: "#4192D9 ",
  indexLabel: "{y}",
  dataPoints: [
    { x: new Date(2009, 0), y: 15 },
    { x: new Date(2010, 0), y: 15 },
    { x: new Date(2011, 0), y: 12 },
    { x: new Date(2012, 0), y: 10 },
    { x: new Date(2013, 0), y: 12 },
    { x: new Date(2014, 0), y: 10 }
  ]
},

我查看了 Canvas JS 网站,他们使用了以下 Ajax 调用示例:

var dataPoints = [];
$.getJSON("https://canvasjs.com/services/data/datapoints.php?xstart=1&ystart=10&length=100&type=json", function(data) {  
    $.each(data, function(key, value){
        dataPoints.push({x: value[0], y: parseInt(value[1])});
    });
    var chart = new CanvasJS.Chart("chartContainer",{
        title:{
            text:"Rendering Chart with dataPoints from External JSON"
        },
        data: [{
        type: "line",
            dataPoints : dataPoints,
        }]
    });
    chart.render();
});

不确定这是否会有所不同,但在上面的 AJAX 调用中使用的 JSON 数据的格式是:

[[1,12],[2,7],[3,6],[4,6],[5,9],[6,13],[7,12],[8,15],[9,14],[10,18]]

【问题讨论】:

    标签: javascript jquery json canvasjs


    【解决方案1】:

    编辑
    https://jsfiddle.net/uvac6rdz/2/
    编辑数据格式以匹配 OP 的首选axisX。

    var chart = new CanvasJS.Chart("chartContainer", {
      title: {
        text: "Composition of Internet Traffic in North America",
        horizontalAlign: "right"
      },
      axisX: {
        tickThickness: 0,
        interval: 1,
        intervalType: "year"
      },
      animationEnabled: true,
      toolTip: {
        shared: true
      },
      axisY: {
        lineThickness: 0,
        tickThickness: 0,
        interval: 20
      },
      legend: {
        verticalAlign: "center",
        horizontalAlign: "left"
      },
    
      data: [
      {
        name: "Real-Time",
        showInLegend: true,
        type: "column",
        color: "#004B8D ",
        indexLabel: "{y}",
        dataPoints: [
        { x: new Date(2009,0), y: 12 },
        { x: new Date(2010,0), y: 7 },
        { x: new Date(2011,0), y: 6}, 
        { x: new Date(2012,0), y: 6}, 
        { x: new Date(2013,0), y: 9}, 
        { x: new Date(2014,0), y: 13}, 
        { x: new Date(2015,0), y: 12}, 
        { x: new Date(2016,0), y: 15}, 
        { x: new Date(2017,0), y: 14}
        ]
      },
    
      ]
    });
    
    chart.render();
    

    我更改了以下内容:

    1. axisX.intervalType 上的类型从 "year""number"
      (由于您的样本数据)。

    2. [[1,12],[2,7]... 中的示例数据格式化为关注
      [{x: 1, y: 12}, {x: 2, y: 7},...

    3. 图表类型从stackedColumn100column

    查看 canvasJS 的文档,该示例要求您将数据格式化为以下内容: {x: valueX, y: valueY}

    【讨论】:

    • 这对我来说效果很好并且很有意义。但是如果我想在我的 JSON 文件中使用日期呢?目前我的 Json 文件类似于[[1,12],[2,7],[3,6]] 我想使用类似于[[2009,0,12],[2010,0,7],[2011,0,6]] 的日期当我使用日期时会添加小数点,因为我的 JSON 读取为数字
    • 我是否可以在我的 JSON 文件中仍然使用以下格式。 { x: new Date(2009, 0), y: 15 }... 我想如果我这样做,我可能需要将我的“x”数据点转换为日期? var dataPoints = json.map(function (p) { p.x = new Date(p.x); return p; }); ?
    • 是的,没关系,您还需要将酸性类型更改为日期(如年份)
    • @Denis Tsoi 谢谢
    • @Denis Tsoi 当然。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多