score:1
By Using This Code We can change the colour of each bar of the graph
{% extends 'base.html' %}
{% block content %}
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
</head>
<body>
<canvas id="myChart" width="400" height="400"></canvas>
<input type="color" id ="colorPicker" hidden/>
</body>
<script>
var color = '#456212';
var lastIndex = null;
var canvas = document.getElementById("myChart");
var myChart = new Chart(canvas.getContext('2d'), {
type: 'bar',
data: {
labels: [{% for item in labels %}
"{{item}}",
{% endfor %}],
datasets: [{
label: '# of Votes',
data: [{% for item in values %}
{{item}},
{% endfor %}],
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
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
canvas.addEventListener("click", ChangeColor, false);
colorPicker = document.getElementById('colorPicker');
colorPicker.addEventListener("change", getColor, false);
function getColor(e){
//
console.log(this.lastIndex)
myChart.data.datasets[0].backgroundColor[lastIndex] = e.target.value;
console.log(e.target.value);
myChart.update();
}
function ChangeColor(e){
e.preventDefault();
var activePoints = myChart.getElementsAtEvent(e);
lastIndex = activePoints[0]._index;
this.color = myChart.data.datasets[0].backgroundColor[lastIndex];
console.log('Before', this.color);
var colorPicker = document.getElementById('colorPicker').click();
//console.log('After',this.color);
}
</script>
{% endblock %}
score:2
I have found the code in javascript to accomplish this task
{% extends 'base.html' %}
{% block content %}
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
</head>
<body>
<canvas id="myChart" width="400" height="400"></canvas>
<input type="color" id ="colorPicker"/>
</body>
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [{% for item in labels %}
"{{item}}",
{% endfor %}],
datasets: [{
label: '# of Votes',
data: [{% for item in values %}
{{item}},
{% endfor %}],
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
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
var colorPicker = document.getElementById("colorPicker");
colorPicker.addEventListener("change", updateChart, false);
function updateChart(e) {
e.preventDefault();
myChart.data.datasets[0].backgroundColor[0] = e.target.value;
myChart.data.datasets[0].backgroundColor[1] = e.target.value;
myChart.data.datasets[0].backgroundColor[2] = e.target.value;
myChart.data.datasets[0].backgroundColor[3] = e.target.value;
myChart.data.datasets[0].backgroundColor[4] = e.target.value;
myChart.data.datasets[0].backgroundColor[5] = e.target.value;
myChart.data.datasets[0].backgroundColor[6] = e.target.value;
myChart.data.datasets[0].backgroundColor[7] = e.target.value;
myChart.update();
}
</script>
{% endblock %}
Source: stackoverflow.com
Related Query
- How to change the chart line or area colors according to the user need?
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- How to plot a line chart which has both the start and points outside the canvas area using Chart.js
- How can I change the background colors of a bar chart after it has been created?
- How do I change the 'months' language displayed on the date axis in Chart JS?
- How to align Chart.JS line chart labels to the center
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How can I remove extra whitespace from the bottom of a line chart in chart.js?
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- How to add area with break on line chart with fill color
- How do I change the colour of a Chart.js line when drawn above/below an arbitary value?
- change stroke line color in chart according to datasets in react native
- angular-chart.js doughnut chart : How do I change width of the arc of a doughnut?
- how to highlight a specific area on chartjs line chart
- How to change background color of labels in line chart from chart.js?
- How to style a pie chart in chart js? I want to change the border color, border width and give them shadow
- how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?
- How to change color by clicking on the chart bar?
- Chartjs - how to change the notation of doughnut chart
- How to reduce the number of points shown on line chartjs chart with a large dataset?
- How do you make a progressive line chart with time as the X axis?
- How do I remove the ticks or inner circles of my polar area chart Chart.js
- How can I change the cursor on pie chart segment hover in ChartJS 3?
- How to create a line chart indicating which month a user wrote more or less blogs?
- How do I change the grid line style on the Y axis in Chart.js?
- Angular-Chart-JS - Line chart with different fill colors according to points' range
- How to get rid of the line on top of every point on a line chart (Chart.js)
- Chart.js how to show line chart without displaying the labels on xaxis and yaxis
More Query from same tag
- ChartJS Line Chart - Points Connected out of order for Timeseries in Angular 6
- Chartjs-2(React), Lines do not appear
- 404 Not Found Error when trying to display a chart with Chart.js
- Chart JS : Getting issue with draw bar chart
- Chart.js custom tooltipTemplate index
- Uncaught ReferenceError: Chart is not defined in Laravel mix
- Split assoc array by keys
- Chart.js - Bar chart: Add text after numbers when h:over a Bar
- Automatically show 0 sales for the date which is not in database
- Chartjs not working with d3 from csv source
- Chart.js v3: How can I grow fontsize of legend of my graph?
- Put sum of values in center of doughnut chart?
- How to reload a jQuery element when a button is pressed
- Chart from Database
- How to set vertical lines for new day on x-axis in ChartJS v3.x
- Mouse-over function behaving weird in Chart.js
- Chart.js different time datasets
- How to hide a dataset with your own button?
- Chart.js dataset controller 'null' when chart drawn
- Chart.js - if there is not value show 0
- Can't reuse a template in Vue.js
- Grouped Bar Chart ChartJS
- React render Chart.js based on api response
- How to change the Y-Axis to show $ in graph
- chart.js chart failing to render
- injecting chart.js sparkline to jqxGrid widget in Angular with typescript
- Is there a way to change part of the title to another font style or set a custom title format in Chart.js?
- Change Legend Box Outline Chart.js 2
- how to display large number of data points
- Rails and Chart.js