I need drill down functionality with chart.js and angular 8. I could get a basic pie chart work and need a drill down chart now. Below is my code.
export class PieChartComponent implements AfterViewInit {
PieChart=[];
constructor() { }
ngAfterViewInit() {
console.log(this.pieElementRef+"VIEW initialize");
this.PieChart = new Chart('pieElement', {
// The type of chart we want to create
type: 'pie',
// The data for our dataset
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'My First dataset',
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: [0, 10, 5, 2, 20, 30, 45]
}]
},
// Configuration options go here
options: {
}
});
}
public handleClick(event) {
// Console.log("text: "+event);
console.log(event);
}
}
html code:
<div class="chart">
<canvas id="pieElement" (click)="handleClick($event)"></canvas>
</div>
I could get a basic pie chart with above code. Now I want to handle click functionality to drill down child pie chart.
Related
I have the following chart:
How can I make sure that the the blue line isn't filled, but at the same time, stays on top of the filled red line?
Taking the line chart example from the documentation, I have tried playing around with the drawTime property, but as you can see, the blue line is on the foreground of the red one, but it is filled (I expect the blue line to be on the foreground, but not filled).
Here's what I have so far:
Data:
const data = {
labels: generateLabels(),
datasets: [
{
label: 'Dataset 1',
data: generateData(),
borderColor: Utils.CHART_COLORS.red,
backgroundColor: Utils.CHART_COLORS.red,
fill: true
},
{
label: 'Dataset 2',
data: generateData(),
borderColor: Utils.CHART_COLORS.blue,
backgroundColor: Utils.transparentize(Utils.CHART_COLORS.blue),
// changed `fill` propery from `true` to `false`
fill: false
}
]
};
Config:
const config = {
type: 'line',
data: data,
options: {
plugins: {
filler: {
propagate: false,
// added `drawTime` property here
drawTime: "beforeDraw",
},
title: {
display: true,
text: (ctx) => 'drawTime: ' + ctx.chart.options.plugins.filler.drawTime
}
},
pointBackgroundColor: '#fff',
radius: 10,
interaction: {
intersect: false,
}
},
};
Setup:
const inputs = {
min: -100,
max: 100,
count: 8,
decimals: 2,
continuity: 1
};
const generateLabels = () => {
return Utils.months({count: inputs.count});
};
Utils.srand(3);
const generateData = () => (Utils.numbers(inputs));
This seems like a bug in chart.js itself.
You have 3 options:
wait until it is fixed within chart.js itself
write your own custom plugin that does the filling
make use of the order property so the blue line gets drawn on the canvas after the red one like so:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: 'red',
borderColor: 'red',
fill: true,
order: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderColor: 'blue',
order: 0
}
]
},
options: {}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script>
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>
EDIT:
After looking at the source code it seems indeed like its a bug in chart.js, it does not check if fill has been set to false so it will always fill with the non default modes. Will put in a fix for this so it will be fixed in version 3.8.1
I am trying to render a line graph with two datasets
My sample chart
<canvas id="myChart"></canvas>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: 'My First dataset',
borderColor: 'rgb(255, 99, 132)',
fill: false,
data: [9000, 9020, 9010, 9004, 9011, 9040, 9050]
},
{
label: 'My Second dataset',
borderColor: 'rgb(0, 99, 132)',
fill: false,
data: [1000, 1020, 1010, 1004, 1011, 1040, 1050]
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
stepSize: 10
}
}]
}
}
});
</script>
This is how it looks currently current result
This the graph will be easy to read if it has axis break like this
expected result
There is no build in way to do this. The best thing you can do is use a second Y axes and link the second dataset to the other axes.
datasets: [
{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1,
yAxisID: 'first'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1,
yAxisID: 'second'
}
]
options: {
scales: {
yAxes: [{
id: 'first'
},{
id: 'second'
}]
}
}
I want to use the autoSkip option in order to limit the number of labels displayed, but I would like the first and the last labels are always shown.
scales: {
xAxes: [{
type: 'category',
ticks: {
autoSkip": true,
maxTicksLimit: 10
}
}]
}
With the above options the tick is limited to 10, the first label is shown, but the there isn't the last one.
From what I understand, the first label is always printed.
Here's a chart example
I'm using ng2-charts for Angular.
The following are the relevant portions of HTML and js.
public options = { "responsive":true,
"scales": {
"xAxes": [{
"bounds": 'ticks',
"type": 'category',
"ticks": {
"autoSkip": true,
"maxTicksLimit": 10
}
}]
}
};
public dataSets: ChartDataSets[] = [{ data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },];
public labels: Label[] = ['01/01/2020', '02/01/2020', '03/01/2020', '04/01/2020', '05/01/2020', '06/01/2020', '07/01/2020'];
<div style="display: block">
<canvas baseChart
[chartType]="'bar'"
[datasets]="dataSets"
[labels]="labels"
[options]="options"
></canvas>
</div>
How can I center the X-axis of a Chart.js (v2.8.0) line chart when there is only one X-axis value?
<span style="width:50%; display:inline-block;">
<canvas id="myChart"></canvas>
</span>
<script>
var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan'],
datasets: [
{ borderColor: 'green', data: [5], label: 'SET1' },
{ borderColor: 'red', data: [7], label: 'SET2' },
{ borderColor: 'blue', data: [3], label: 'SET3' }
]
}
});
</script>
See Chartjs linechart with only one point - how to center
You can add dummy labels to center your single point:
{
type: 'line',
data: {
labels: ['', 'Jan', ''],
datasets: [
{ borderColor: 'green', data: [null, 5, null], label: 'SET1' },
{ borderColor: 'red', data: [null, 7, null], label: 'SET2' },
{ borderColor: 'blue', data: [null, 3, null], label: 'SET3' }
]
}
}
Or you can use the scatter chart type to manually specify the x coordinate of your points.
I am able to create a chart.js pie chart below:
this.httpClient.get(this.url).subscribe((res: Stock[]) => {
res.forEach(holding => {
this.company.push(holding.company);
this.price.push(holding.price);
});
this.chart = new Chart('canvas', {
type: 'pie',
data: {
labels: this.company,
datasets: [
{
data: this.price,
borderColor: '#3cba9f',
backgroundColor: 'rgba(35, 206, 107, 0.5)',
fill: false
}
]
},
options: {
legend: {
display: true
}
}
});
});
At the moment, each segment of the pie chart has the same backgroundColor, as I have assigned it above.
My issue is that I want each segment of the pie to have a different background color. Can someone please tell me how I can make this change with the above code?
You can pass an array of strings (colours) to backgroundColor:
backgroundColor: ['red', 'green', 'blue']
Example:
new Chart(document.getElementById("chart"), {
type: "pie",
data: {
datasets: [{
data: [1, 2, 3],
backgroundColor: ['red', 'green', 'blue']
}]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<canvas id="chart"></canvas>