jquery - 具有多个数据和各个数据点作为图例的堆叠条形图

标签 jquery d3.js charts

我正在尝试创建一个堆积条形图。 x 上的每个标签都有不同的堆叠数据点。有些数据点在 x 轴上重叠,有些数据点仅对于特定标签是唯一的。
我尝试在融合图表( http://jsfiddle.net/fu2pprmk/ )和 d3 js 中。我在 d3.js 中遇到的最接近的( http://jsfiddle.net/wz1cwrLL/ ) 当我尝试在缺少数据点的情况下实现它时,出现错误

Error: attribute y: Expected length, "NaN".

Error: attribute height: Expected length, "NaN".

看来我需要检查缺失值。对 d3js 完全不了解。任何输入都有帮助。

我的数据示例就在这个 fiddle 中。

http://jsfiddle.net/wz1cwrLL/1/

或者,这可以在 rCharts 中实现吗?

最佳答案

上面使用 FusionCharts“stackedcolumn2d”图表提供的 fiddle 在各个数据对象中定义了“颜色”属性,用于设置数据图颜色。

每个图例代表每个数据集系列。因此,为该系列设置的颜色将反射(reflect)在该系列的图例中。为此,您需要在数据集级别设置“颜色”属性。

请注意:图例不能代表单个数据图

请引用这里的 fiddle :http://jsfiddle.net/Akash008/dty7dfzk/4/

FusionCharts.ready(function() {
var revenueChart = new FusionCharts({
type: 'stackedcolumn2d',
renderAt: 'chart-container',
width: '500',
height: '300',
dataFormat: 'json',
dataSource: {
  "chart": {
    "caption": "Revenue split by product category",
    "subCaption": "For current year",
    "xAxisname": "Quarter",
    "yAxisName": "Revenues (In USD)",
    //"paletteColors": "#FF0000,#00FF00,#0000FF,#FFFF00",
    "showSum": "1",
    "numberPrefix": "$",
    "showLegend": "1",
    "theme": "fint",
    "legendPosition": "right",
    "legendCaption": "food Items",
    "legendScrollBgColor": "#cccccc",
    "legendScrollBarColor": "#999999",
    "plotHighlightEffect": "fadeout",
  },

  "categories": [{
    "category": [{
      "label": "Non-dairy Product"
    }, {
      "label": "Vegetables"
    }, {
      "label": "Fruits"
    }, {
      "label": "Vegetables"
    }]
  }],

  "dataset": [{
    "seriesname": "Food Products",
    "color": "#FF0000",
    "data": [{
      "toolText": "Egg",
      "value": "11000"
    }, {
      "toolText": "Potato",
      "value": "15000"
    }, {
      "toolText": "Apple",
      "value": "13500"
    }, {
      "toolText": "lettuce",
      "value": "15000"
    }]
  }, {
    "seriesname": "Non-Food Products",
    "color": "#00FF00",
    "data": [{
      "toolText": "Meat",
      "value": "11400"
    }, {
      "toolText": "Eggplant",
      "value": "14800"
    }, {
      "toolText": "Oranges",
      "value": "8300"
    }, {
      "toolText": "cilantro",
      "value": "11800"
    }]
  }, {
    "seriesname": "Non-Food Products",
    "color": "#0000FF",
    "data": [{
      "toolText": "Fish",
      "value": "11400"
    }, {
      "toolText": "Carrot",
      "value": "14800"
    }, {
      "toolText": "plums",
      "value": "8300"
    }, {
      "toolText": "Kale",
      "value": "11800"
    }]
  }, {
    "seriesname": "Non-Food Products",
    "color": "#FFFF00",
    "data": [{
      "toolText": "Fish",
      "value": "11400"
    }, {
      "toolText": "Carrot",
      "value": "14800"
    }, {
      "toolText": "plums",
      "value": "8300"
    }, {
      "toolText": "Kale",
      "value": "11800"
    }]
  }],
}
})
    revenueChart.render();
});

您还可以通过设置图表级别属性“paletteColors”来一次性设置所有系列的颜色来实现相同的效果。引用 fiddle :http://jsfiddle.net/Akash008/dty7dfzk/3/

FusionCharts.ready(function() {
var revenueChart = new FusionCharts({
type: 'stackedcolumn2d',
renderAt: 'chart-container',
width: '500',
height: '300',
dataFormat: 'json',
dataSource: {
  "chart": {
    "caption": "Revenue split by product category",
    "subCaption": "For current year",
    "xAxisname": "Quarter",
    "yAxisName": "Revenues (In USD)",
    "paletteColors": "#FF0000,#00FF00,#0000FF,#FFFF00",
    "showSum": "1",
    "numberPrefix": "$",
    "showLegend": "1",
    "theme": "fint",
    "legendPosition": "right",
    "legendCaption": "food Items",
    "legendScrollBgColor": "#cccccc",
    "legendScrollBarColor": "#999999",
    "plotHighlightEffect": "fadeout",
  },

  "categories": [{
    "category": [{
      "label": "Non-dairy Product"
    }, {
      "label": "Vegetables"
    }, {
      "label": "Fruits"
    }, {
      "label": "Vegetables"
    }]
  }],

  "dataset": [{
    "seriesname": "Food Products",
    "data": [{
      "toolText": "Egg",
      "value": "11000"
    }, {
      "toolText": "Potato",
      "value": "15000"
    }, {
      "toolText": "Apple",
      "value": "13500"
    }, {
      "toolText": "lettuce",
      "value": "15000"
    }]
  }, {
    "seriesname": "Non-Food Products",
    "data": [{
      "toolText": "Meat",
      "value": "11400"
    }, {
      "toolText": "Eggplant",
      "value": "14800"
    }, {
      "toolText": "Oranges",
      "value": "8300"
    }, {
      "toolText": "cilantro",
      "value": "11800"
    }]
  }, {
    "seriesname": "Non-Food Products",
    "data": [{
      "toolText": "Fish",
      "value": "11400"
    }, {
      "toolText": "Carrot",
      "value": "14800"
    }, {
      "toolText": "plums",
      "value": "8300"
    }, {
      "toolText": "Kale",
      "value": "11800"
    }]
  }, {
    "seriesname": "Non-Food Products",
    "data": [{
      "toolText": "Fish",
      "value": "11400"
    }, {
      "toolText": "Carrot",
      "value": "14800"
    }, {
      "toolText": "plums",
      "value": "8300"
    }, {
      "toolText": "Kale",
      "value": "11800"
    }]
  }],


}
  })
  revenueChart.render();
});

希望这有帮助。

关于jquery - 具有多个数据和各个数据点作为图例的堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841386/

相关文章:

javascript - 如何一个接一个地运行一个脚本?

javascript - 如何旋转D3图表,使选中的圆弧在底部?

javascript - d3.behavior.zoom 鼠标滚轮缩放中心动态偏移

javascript - d3 v4刷范围错误: <rect> attribute x: Expected length, "NaN"

c++ - 如何绘制 C++ Win32 控制台应用程序的输出?

excel - 使用第 1 行合并单元格中的文本为多个图表提供标题

javascript - Apexcharts - 悬停在一个非常小的列上

javascript - 在页面加载后通过在 head 中附加脚本来加载 Google Analytics 并不总是有效

javascript - 如何将值传递给父页面上的 javascript 函数?

javascript - 如何使用 bootstrap 或 js 在数据列表中设置滚动条?