如何在chart.js中从JSON创建堆积条形图

时间:2017-08-31 04:40:45

标签: javascript json chart.js bar-chart

我想创建堆积条形图,但我不知道如何做到这一点 我不知道json将价值放在图表中 我有以下Json数据:

var setdata = [{
      "data": [{
              "month" : "January",
               "name" : "Alex",
               "count" : 10
      }]
    },
     {
      "data": [{
              "month" : "February",
               "name" : "Alex",
               "count" : 20
     },
     {  
      "data": [{
              "month" : "February",
               "name" : "John",
               "count" : 30
     },
     {  
      "data": [{
              "month" : "February",
               "name" : "Mark",
               "count" : 40
     }]
              },
    {
      "data": [{
              "month" : "March",
               "name" : "Alex",
               "count" : 10
     },
     {  
              "month" : "March",
               "name" : "John",
               "count" : 20
     }]
    }
    ]

我想创建堆积条形图:

|                _
|               | | Mark
|               |_|
|               | |                   _
|               | | John             | |
|               |_|                  | | John
|               | |                  |_|
|  _            | |                  | |
| | |Alex       | | Alex             | | Alex
|_|_|___________|_|__________________|_|_______
 January      February              March
                Months
请帮助我。 谢谢。 bar chart in chart.js

2 个答案:

答案 0 :(得分:1)

您可以尝试FusionCharts,以使您的制图过程愉快。它允许您立即创建图表,而无需广泛了解数据结构。我是否提到图表看起来很漂亮?

使用FusionCharts Stacked Charts,我已使用您的数据在JSFiddle为您创建了一个示例。

FusionCharts.ready(function() {


 var revenueChart = new FusionCharts({
    type: 'stackedcolumn2d',
    renderAt: 'chart-container',
    width: '550',
    height: '450',
    dataFormat: 'json',
    dataSource: {
      "chart": {
        "caption": "Alex-John-Mark",
        "subCaption": "",
        "xAxisname": "Month",
        "yAxisName": "Units",

    "paletteColors": "#4A148C,#004D40, #FF6F00",
    "bgColor": "#ffffff",
    "borderAlpha": "20",
    "showCanvasBorder": "0",
    "usePlotGradientColor": "0",
    "plotBorderAlpha": "10",
    "legendBorderAlpha": "0",
    "legendShadow": "0",
    "valueFontColor": "#ffffff",
    "showXAxisLine": "1",
    "xAxisLineColor": "#999999",
    "divlineColor": "#999999",
    "divLineIsDashed": "1",
    "showAlternateHGridColor": "0",
    "subcaptionFontBold": "0",
    "subcaptionFontSize": "14",
    "showHoverEffect": "1"
  },
  "categories": [{
    "category": [{
        "label": "January"
      }, {
        "label": "February"
      }, {
        "label": "March"
      }

    ]
  }],
  "dataset": [{
    "seriesname": "Alex",
    "data": [{
        "value": "10"
      }, {
        "value": "20"
      },

      {
        "value": "10"
      }
    ]
  }, {
    "seriesname": "John",
    "data": [{
        "value": "0"
      }, {
        "value": "30"
      },

      {
        "value": "20"
      }
    ]
  }, {
    "seriesname": "Mark",
    "data": [{
        "value": "0"
      }, {
        "value": "40"
      },

      {
        "value": "0"
      }
    ]
  }]


  }
  }).render();
});

如果这解决了您的目的,请告诉我。

答案 1 :(得分:0)

为此,您可以使用chart.js 我想,这可能会有所帮助