There are many examples of extending a Chart to include a line, both horizontal and vertical. However, I have not found a way to draw a vertical line with a horizontal bar chart.
有许多扩展图表的例子,包括水平线和垂直线。然而,我还没有找到用水平条形图画一条垂线的方法。
- Horizontal line on horizontal line chart
- 水平线图上的水平线
- Vertical line on horizontal line chart
- 水平线图上的垂直线
- Horizontal line on vertical bar chart
- 在垂直条形图上的水平线
There is not a "vertical line chart" option like there is a "horizontal bar chart" option. How do I combine a horizontal bar chart with a vertical line?
没有一个“垂直线图表”选项,就像有一个“水平条形图”选项。如何将水平条形图与垂直线结合?
图表。js文件
The result would have a bar dataset and a line dataset that can be used on the same chart using the same axis like below:
结果将有一个bar数据集和一个行数据集,可以使用相同的轴在相同的图表上使用:
2 个解决方案
#1
6
ChartJS supports custom plugins. Create a plug in that will read a new property from the chart options and draw the line at the specified index.
ChartJS支持自定义插件。在其中创建一个插件,它将从图表选项中读取一个新属性,并在指定的索引处画一条线。
看到它在Plunkr
//Create the plug in
var originalLineDraw = Chart.controllers.horizontalBar.prototype.draw;
Chart.helpers.extend(Chart.controllers.horizontalBar.prototype, {
draw: function () {
originalLineDraw.apply(this, arguments);
var chart = this.chart;
var ctx = chart.chart.ctx;
var index = chart.config.options.lineAtIndex;
if (index) {
var xaxis = chart.scales['x-axis-0'];
var yaxis = chart.scales['y-axis-0'];
var x1 = xaxis.getPixelForValue(index);
var y1 = yaxis.top;
var x2 = xaxis.getPixelForValue(index);
var y2 = yaxis.bottom;
ctx.save();
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.strokeStyle = 'red';
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.restore();
}
}
});
//Set up the chart data
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1,
data: [65, 59, 80, 81, 56, 55, 40],
}
]
};
//Load Chart
var ctx = $("#myChart");
var myBarChart = new Chart(ctx, {
type: 'horizontalBar',
data: data,
options: {
//Set the index of the value where you want to draw the line
lineAtIndex: 60,
legend: {
display: false
}
}
});
<canvas id="myChart"></canvas>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.min.js'></script>
<script src="horizontalBarPlugin.js"></script>
<script src="buildChart.js"></script>
#2
0
Have you tried this? It's a horizontal line with vertical bar charts - so the opposite of you case. But maybe you can derive something useful out of it:
你有试过吗?这是一条横线,有垂直的条形图——所以你的情况正好相反。但是也许你可以从中得到一些有用的东西:
var chart = c3.generate({
bindto: '#chartContainer',
tooltip: {
grouped: false
},
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 130, 340, 200, 500, 250, 350],
['data3', 30, 200, 100, 400, 150, 250],
['data4', 130, 340, 200, 500, 250, 350],
['data5', 130, 340, 200, 500, 250, 350],
['data6', 130, 340, 200, 500, 250, 350],
['data7', 130, 340, 200, 500, 250, 350],
['diif1', null, null, 700 ],
['diif2', null, null, 1200]
],
types:{
"data1" :"bar",
"data2" :"bar",
"data3" :"bar",
"data4" :"bar",
"data5" :"bar",
"data6" :"bar",
"data7" :"bar",
"diff1" : "line",
"diff2" : "line"
},
order:null,
groups: [
['data1', 'data2','data3', 'data4'],
['data5', 'data6','data7']
],
onclick: function (d, element) {
var name=d.name;
drilldown(name);
}
},
grid: {
y: {
lines:[{value:1400,text:1400},
{value: 1450,text: 1450}
]
}
}
});
function drilldown(name){
alert('Call for ' +name);
}
http://jsfiddle.net/9nxcfzb9/12/
http://jsfiddle.net/9nxcfzb9/12/
I'm looking for something similar to you but not with horizontal bar charts, but timespan bars, if you've got an idea, please let me know: How do I change or add the vertical line on a tooltip of a c3 graph?
我在找一些类似的东西,但不是水平条形图,但是timespan酒吧,如果你有一个想法,请告诉我:我如何改变或在c3图形的工具提示上添加垂线?
#1
6
ChartJS supports custom plugins. Create a plug in that will read a new property from the chart options and draw the line at the specified index.
ChartJS支持自定义插件。在其中创建一个插件,它将从图表选项中读取一个新属性,并在指定的索引处画一条线。
看到它在Plunkr
//Create the plug in
var originalLineDraw = Chart.controllers.horizontalBar.prototype.draw;
Chart.helpers.extend(Chart.controllers.horizontalBar.prototype, {
draw: function () {
originalLineDraw.apply(this, arguments);
var chart = this.chart;
var ctx = chart.chart.ctx;
var index = chart.config.options.lineAtIndex;
if (index) {
var xaxis = chart.scales['x-axis-0'];
var yaxis = chart.scales['y-axis-0'];
var x1 = xaxis.getPixelForValue(index);
var y1 = yaxis.top;
var x2 = xaxis.getPixelForValue(index);
var y2 = yaxis.bottom;
ctx.save();
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.strokeStyle = 'red';
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.restore();
}
}
});
//Set up the chart data
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1,
data: [65, 59, 80, 81, 56, 55, 40],
}
]
};
//Load Chart
var ctx = $("#myChart");
var myBarChart = new Chart(ctx, {
type: 'horizontalBar',
data: data,
options: {
//Set the index of the value where you want to draw the line
lineAtIndex: 60,
legend: {
display: false
}
}
});
<canvas id="myChart"></canvas>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.min.js'></script>
<script src="horizontalBarPlugin.js"></script>
<script src="buildChart.js"></script>
#2
0
Have you tried this? It's a horizontal line with vertical bar charts - so the opposite of you case. But maybe you can derive something useful out of it:
你有试过吗?这是一条横线,有垂直的条形图——所以你的情况正好相反。但是也许你可以从中得到一些有用的东西:
var chart = c3.generate({
bindto: '#chartContainer',
tooltip: {
grouped: false
},
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 130, 340, 200, 500, 250, 350],
['data3', 30, 200, 100, 400, 150, 250],
['data4', 130, 340, 200, 500, 250, 350],
['data5', 130, 340, 200, 500, 250, 350],
['data6', 130, 340, 200, 500, 250, 350],
['data7', 130, 340, 200, 500, 250, 350],
['diif1', null, null, 700 ],
['diif2', null, null, 1200]
],
types:{
"data1" :"bar",
"data2" :"bar",
"data3" :"bar",
"data4" :"bar",
"data5" :"bar",
"data6" :"bar",
"data7" :"bar",
"diff1" : "line",
"diff2" : "line"
},
order:null,
groups: [
['data1', 'data2','data3', 'data4'],
['data5', 'data6','data7']
],
onclick: function (d, element) {
var name=d.name;
drilldown(name);
}
},
grid: {
y: {
lines:[{value:1400,text:1400},
{value: 1450,text: 1450}
]
}
}
});
function drilldown(name){
alert('Call for ' +name);
}
http://jsfiddle.net/9nxcfzb9/12/
http://jsfiddle.net/9nxcfzb9/12/
I'm looking for something similar to you but not with horizontal bar charts, but timespan bars, if you've got an idea, please let me know: How do I change or add the vertical line on a tooltip of a c3 graph?
我在找一些类似的东西,但不是水平条形图,但是timespan酒吧,如果你有一个想法,请告诉我:我如何改变或在c3图形的工具提示上添加垂线?