score:11
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.
//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>
score: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/
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?
Source: stackoverflow.com
Related Query
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- How to draw Horizontal line on Bar Chart Chartjs
- Draw stacked horizontal bar chart with Average line using ChartJS
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- How to add vertical line in bar chart using chartJs and ng2-charts?
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Draw a horizontal and vertical line on mouse hover in chart js
- Chart Js V2 draw Horizontal Bar(Average) Over Vertical Bar
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- ChartJS: how to make a bar chart with a horizontal guideline:
- Chart.js - Horizontal line on Bar chart interferes with tooltip
- Chart.js - draw horizontal line in Bar Chart (type bar)
- How to reduce the number of points shown on line chartjs chart with a large dataset?
- How do I create a stacked horizontal bar chart with Chart.js in React?
- ChartJS 2.9.4 can't overlay line data on Horizontal Bar chart
- Can't Draw Horizontal Line on Graph Using ChartJS Annotation Plugin and Primevue Chart
- Vertical Line chart with ChartJS
- ChartJs - stacked bar chart with groups - how to create second x-axis with stack id
- Chartjs 3 how to draw a horizontal line starting at a specific yAxis value
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- Chart.js Draw a Stacked Bar Chart with Limit Line
- How to order a horizontal bar chart - chartJS & Chart.HorizontalBar.js
- ChartJs How to display horizontal and vertical lines through the datasets points with their values on axes?
- How to draw a horizontal line over bar columns in Chart.js that's started from X axis?
- How do I draw horizontal bars with a label using either ChartJS or D3?
- Draw horizontal line on chart in chart.js on v2
More Query from same tag
- chartjs hover over data without hoveringing on line
- chartjs plugin datalabels does not show value on charts
- ChartJS charts not generating within tabs
- Chart JS - set start of week for x axis time series
- HTML5 Canvas expanding the draw area after drawing
- Chart.js: refresh second Y axis step on legend click
- How to make a line dashed or dotted in chartJS?
- Chart.JS Data Element To Be settled as Euro Currency
- Updating Chartjs Line chart from database
- charts.js from session var, flicker effect
- Moving vertical line when hovering over the chart using chart.js
- Graph getting messed up when user resizes page
- Chart.js charts not rendering data until I inspect element, is it because of async?
- How to pass values to a chart (chart.js / morris.js)
- Import data from Excel and use in Chart.js
- vue-chart.js : Data available as computed properties, but not showing in chart
- Getting dynamic data for chart.js from Django model
- Wrong label value is displayed on point hover - Chart.JS
- Chart.js find visible data points following zoom
- How can have variable data be from a url in javascript
- Wrong tootip in chartjs
- chart js (version 2) bar chart superimpose one data set onto another
- Draw points and lines inside a bar in bar chart
- Chart JS: Ignoring x values and putting point data on first available labels
- ChartJS in React throws 'too may re-renders'
- Chart.js 3.6.2, Angular 10: Logarithmic Line chart Y-Axis problems. How to set and keep Y-Axis properties, even when data changes?
- Chartjs doesn't update dataset label on tooltips
- Send data from a form to another component of the same level -angular
- ChartJS - Override Attributes (AngularJS)
- How to use two datasets in chart.js doughnut chart?