score:5
Accepted answer
You can change the color of a bar element after you have created your chart.
After new Chart()
statement you can access and modify the chart element properties and update the chart like this :
var wpChartChartTitleBar = new Chart(document.getElementById("myChart").getContext("2d")).Bar(ChartTitleData, ChartTitleOps);
// Change 2nd bar to red (display).
wpChartChartTitleBar.datasets[0].bars[1].fillColor = "rgba(229,12,12,0.7)";
wpChartChartTitleBar.datasets[0].bars[1].strokeColor = "rgba(229,12,12,1)";
// Change 2nd bar to red (highlight setting on mouse over)
wpChartChartTitleBar.datasets[0].bars[1].highlightFill = "rgba(0,229,0,0.7)";
wpChartChartTitleBar.datasets[0].bars[1].highlightStroke = "rgba(0,229,0,1)";
wpChartChartTitleBar.update();
See a fiddle of it here.
score:4
Since rtome's method doesn't seem to work in newer versions of Chart.js, here's a working example of differing bar colours for the current version (2.9.3 as of this post).
var ctx = document.getElementById("myChart").getContext("2d");
var chart = new Chart(ctx, {
// The type of chart we want to create
type: "horizontalBar",
// The data for our dataset
data: {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
backgroundColor: [
"rgb(255, 99, 132)",
"rgb(255, 159, 64)",
"rgb(255, 205, 86)",
"rgb(75, 192, 192)",
"rgb(54, 162, 235)",
"rgb(153, 102, 255)",
"rgb(201, 203, 207)"
],
borderColor: "rgb(255, 99, 132)",
data: [3, 10, 5, 2, 20, 30, 45]
}]
},
// Configuration options go here
options: {}
});
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
body {
margin-top: 35px;
}
#container {
width: 95%;
margin-left: auto;
margin-right: auto;
margin-bottom: auto;
}
<html>
<head>
<title>Bar colour example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
</head>
<body>
<div id="container">
<canvas id="myChart"></canvas>
</div>
</body>
</html>
Source: stackoverflow.com
Related Query
- chart.js set one bar as different colour?
- chart js (version 2) bar chart superimpose one data set onto another
- How to set different colors in each stacked bar chart cell?
- chart js 2 how to set bar width
- chartjs : how to set custom scale in bar chart
- Chart.js Mixed Bar and Line chart with different scales
- Different color for each column in angular-chartjs bar chart
- Can Chart.js combines Line Chart and Bar Chart in one canvas
- set y-axis scale manually in a bar chart using angular-chart.js
- How to set single color on each bar in angular chart js
- Chart.js bar chart mouse hovering highlights all the datasets instead of just the selected one
- Customize different tooltips of bar chart
- Vue ChartKick - different colours for bar chart
- How to show different product name for every bar in chart js
- Chartjs - Set start and end value into a Bar Chart
- How do I set a bar chart to default to a suggested maximum in chart.js v1.01?
- How to use set the color for each bar in a bar chart using chartjs?
- How to set custom Y Axis Values (Chart.js v2.8.0) in Chart Bar JS
- Set fixed label size for grouped bar chart in angular Chartjs
- Charts.js - Bar chart different colors for value intervals not working
- How to set X coordinate for each bar with react chart js 2?
- Chart Js - Globally set bar chart color based on value
- Set max value of chart.js bar chart
- How do I get a different label for each bar in a bar chart in ChartJS?
- angular chart js set fill color of bar chart
- set scroll on bar chart in chartjs
- Bar chart (chart.js) with only 2 points does not show one of the bars
- How can I set different colours for each bar in angular-chart.js v1.0.0?
- chartjs display data in one bar line with 3 different data sets in 3 different colors
- I need to increase data in a bar chart by one with a button click
More Query from same tag
- How to use HTML in custom tooltip in angular charts?
- Why won't this html page run, Devexpress documentation ChartJS
- The title of my ChartJs on the chart is undefined, but I can't change it because my labels use a function: Object.keys(groupMedia)
- How to update chart when state changes in vue?
- Add a click event to the bar chart of chartjs
- how to change color of dots in graph using chart.js
- How to get any y value for specified x in chart.js?
- How to make bootstrap 4 column height the same as row height
- Is there any way to display float bar in chart.js?
- Can I remove the Y-axis counter on chart.js?
- Including Chart.js in a webpage without node.js and without CDN
- Switching between different Chart.js charts using the same canvas
- Dataset._meta[0].dataset is null in ChartJS
- "Type of import (".../charjs/types/indexsm" has no construct signatures issue on Vue3 and ChartJS?
- Javascript How to give color using conditional statement based on json array state value
- chart.js d3js chart in time scale, data from CSV file
- Template variable injection in JS script
- How to make the background color of the chart as gradient using Chart.js
- How to replay animation chart.js
- Chart.js with JSON data error
- Array value is not being passed to the plugin
- plot line graph in Chartjs 2 when number of data in yaxis is not eqaul to number of data in xaxis
- Remove/Hide (display:none) the graphical chart in small-mobile view - JS chart
- I wanna make this chart using chartjs In reactjs
- My choice of color is not used in my stacked bar chart
- charts.js update global variables
- On hover bar is overlapped by label chartjs
- Chart.js animations ignoring global default configuration
- chart.js datapoint offset multiple charts
- Chart.js stacked and grouped horizontalBar chart