Apexcharts DataLabels - apexcharts

Can anyone help in displaying data labels on apex charts I want to show data value instead of %value
I want to show the value 55 or 41 or something likewise on the data label how to do the formatting for this in data labels block
dataLabels: {
enabled: true,
textAnchor: "start",
style: {
colors: ["#fff"]
},
formatter: function(val, opt) {
return opt.w.globals.labels[opt.dataPointIndex] + ": " + val;
},
offsetX: 0,
dropShadow: {
enabled: true
}
},
var options = {
series: [{
name: 'Marine Sprite',
data: [44, 55, 41, 37, 22, 43, 21]
}, {
name: 'Striking Calf',
data: [53, 32, 33, 52, 13, 43, 32]
}],
};
[![Apex Charts datalabel ][1]][1]

It is similar to how you have raised it in the dataLabels formatter, val contains the value of data.
I leave you below a possible dataLabels and an example in codesandbox
dataLabels: {
enabled: true,
textAnchor: "start",
style: { colors: ["#fff"] },
formatter: function (val, opt) {
console.log("VAL: ", val);
let valueFormatted = val.toFixed(0);
console.log("Val formatted: ", valueFormatted);
// return opt.w.globals.labels[opt.dataPointIndex] + ": " + val;
return valueFormatted;
},
offsetX: 0,
dropShadow: { enabled: true }
}
Result of the solution in photo with first solution with val
You can also invent another value within series and put it in the formatter, in the code is the example with valueInvented, and in the formatter function we access it with opt.w.config.series[opt.seriesIndex].valueInvented
const series = [
{
name: "Marine Sprite",
data: [20, 60, 20, 20, 20, 20, 20],
valueInvented: 1
},
{
name: "Striking Calf",
data: [10, 10, 20, 20, 20, 20, 20],
valueInvented: 2
},
{
name: "Tank Picture",
data: [10, 5, 20, 20, 20, 20, 20],
valueInvented: 3
},
{
name: "Bucket Slope",
data: [30, 5, 20, 20, 20, 20, 20],
valueInvented: 4
},
{
name: "Reborn Kid",
data: [30, 20, 20, 20, 20, 20, 25],
valueInvented: 5
}
];
formatter: function (val, opt) {
console.log("VAL: ", val);
console.log("OPT:", opt);
console.log(
"OPT valueInvented: ",
opt.w.config.series[opt.seriesIndex].valueInvented
);
let valueFormatted = val.toFixed(0);
console.log("Val formatted: ", valueFormatted);
return opt.w.config.series[opt.seriesIndex].valueInvented
},

Just add a custom formatter as below:
dataLabels: {
formater(val, opt) {
return opt.w.globals.series[opt.seriesIndex][opt.dataPointIndex]
}
}

Related

ChartJS 3+ x-axis only showing full moment object instead of just month

I'm trying to show only the month and year in the x-axis of my chart by using momentjs, but it is only putting what appears to be the full moment date in the x-axis.
I have been looking at many examples of people doing this but none of them seem to work in the latest version of chartjs.
I am aware of an "adapter" needed for use with momentjs which I have included but have no way to know if it is working.
Here is a jsfiddle of the code I am using, help would be greatly appreciated. https://jsfiddle.net/7z20jg68/
I also have error telling me Invalid scale configuration for scale: x which is confusing because my x axis seems to be in the correct order..Thanks!
Your X axis scale was indeed invalid, you declared it as an array which is V2 syntax, in V3 all the scales are their own object where the key of the object is the scaleID. So removing the array brackets around the x axis scale object will resolve the issue.
Also your legend and tooltip config where wrong and in the wrong place, also V2 syntax.
For all changes please read the migration guide
const gainedChart = document.getElementById('gained-chart').getContext('2d');
const gain = [74.699997, 76.580002, 81.349998, 83.000000, 85.879997, 83.120003, 77.989998, 81.279999];
const dates = ["Jan 2, 20", "Feb 3, 20", "Mar 4, 20", "Apr 5, 20", "May 6, 20", "Jun 9, 20", "Nov 10, 20", "Dec 11, 20"];
let gainData = {
datasets: [{
label: "Gain (%)",
data: gain,
borderColor: 'rgba(255, 66, 66, 1)',
backgroundColor: 'rgba(255, 66, 66, 1)',
pointRadius: 1.2,
pointBackgroundColor: 'rgba(255, 66, 66, 0.8)',
pointHoverRadius: 6,
pointHitRadius: 20,
pointBorderWidth: 2,
}]
};
let gainConfig = {
plugins: {
legend: {
display: true,
position: 'top',
labels: {
boxWidth: 80,
color: 'black'
},
},
tooltip: {
callbacks: {
label: function(tooltipItem, data) {
return parseInt(tooltipItem.parsed.y)
}
}
},
},
scales: {
x: {
type: 'time',
time: {
minUnit: 'month'
}
},
y: {
suggestedMax: 45,
ticks: {
stepSize: 5,
//max: 100
},
},
},
maintainAspectRatio: false,
responsive: true,
};
let lineGainChart = new Chart(gainedChart, {
type: 'line',
data: gainData,
options: gainConfig,
plugins: [{
beforeInit: function(lineGainChart) {
for (let c = 0; c < dates.length; c++) {
let myMoment = moment(dates[c], 'MMM D, YYYY');
lineGainChart.data.labels.push(myMoment);
}
}
}]
});
<canvas class="graph-canvas" id="gained-chart" width="400" height="400"></canvas>
<script src="https://cdn.jsdelivr.net/npm/moment#2.29.1/moment.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment#^1"></script>

chartjs dataset from single line to multiple line

please help, thank you.
i am now writing datapart of chartjs
var configcount = {type: "line",data: {
labels: ["Apr20", "May20", "Jun20", "Jul20", "Aug20", "Sep20", "Oct20"],
datasets: [{label: "Balance1",
data: [10, 20, 30, 40, 50, 60, 70],
fill: false,pointBorderWidth: ...
,},},],},},};
want to change multiple line that Balance1: 10, 20, 30, 40, 50, 60, 70 & Balance2: 15, 30, 15, 30, 15, 30, 15
You need to define two distinct datasets as follows.
data: {
labels: ["Apr20", "May20", "Jun20", "Jul20", "Aug20", "Sep20", "Oct20"],
datasets: [{
label: "Balance1",
data: [10, 20, 30, 40, 50, 60, 70],
...
}, {
label: "Balance2",
data: [15, 30, 15, 30, 15, 30, 15],
fill: false,
...
}]
}
Please take a loo at below code and see how it works.
new Chart(document.getElementById("chart"), {
type: 'line',
data: {
labels: ["Apr20", "May20", "Jun20", "Jul20", "Aug20", "Sep20", "Oct20"],
datasets: [{
label: "Balance1",
data: [10, 20, 30, 40, 50, 60, 70],
fill: false,
backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgb(255, 99, 132)",
pointBorderWidth: 10
}, {
label: "Balance2",
data: [15, 30, 15, 30, 15, 30, 15],
fill: false,
backgroundColor: "rgba(54, 162, 235, 0.2)",
borderColor: "rgb(54, 162, 235)",
pointBorderWidth: 4,
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="chart" height="80"></canvas>

borderdash options in chart js changes my legend

I added borderdash to my chart to make make the line more distinct but it alter the shape of my legend. I want it to be an square, not that weird rectangle shape in the legend. I have no idea how to fix this. Does anyone know of an solution to this?
Here is my chart js options:
var options = {
responsive: true,
maintainAspectRatio: false,
backgroundColor: "#fffffff",
title: {
display: true,
text: title,
fontSize: 24
},
legend: {
display: true
},
tooltips: {
callbacks: {
label: function(tooltipItem, ctx) {
var label;
if(x_axis == "Temperature [K]")
{
label = [["Pressure: " + tooltipItem.yLabel], ["Temperature: " + tooltipItem.xLabel]];
}
else
{
label = [["Pressure: " + tooltipItem.yLabel], ["Abundance: " + tooltipItem.xLabel]];
}
return label;
},
}
},
animation: {
duration: 1 // general animation time
},
scales: {
xAxes: [{
type: x_type,
display: true,
ticks: {
beginAtZero: false,
autoSkip: false,
fontSize: 20,
suggestedMin: x_min,
suggestddMax: x_max,
callback: function(value, index, values) {
if (x_axis == "Abundance"){
if (value == 1e-9)
{
return 1 + "n";
}
else if(value == 1e-8)
{
return 10 + "n";
}
else if(value == 1e-7)
{
return 100 + "n";
}
else if (value == 1e-6){
return 1 + "\u03BC";
}
else if (value == 1e-5)
{
return 10 + "\u03BC";
}
else if(value == 1e-4)
{
return 100 + "\u03BC";
}
else if (value == .001 || value == .01 || value == .1 || value == 1){
return value;
}
}
else if (x_axis == "Temperature [K]")
{
return value;
}
}
},
scaleLabel: {
labelString: x_axis,
display: true,
fontSize: 20,
fontColor: "Black",
}
}],
yAxes: [{
type: 'logarithmic',
display: true,
ticks: {
reverse: true,
beginAtZero: false,
autoSkip: false,
min: 1e-6,
max: 1,
steps: 6,
fontSize: 20,
callback: function(value, index, values) {
// y_log_ticks = [1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1]
if (value == 1e-6){
return 1 + "\u03BC";
}
else if (value == 1e-5)
{
return 10 + "\u03BC";
}
else if(value == 1e-4)
{
return 100 + "\u03BC";
}
else if (value == .001 || value == .01 || value == .1 || value == 1){
return value;
}
}
},
scaleLabel:{
display: true,
labelString: y_axis,
diplay: true,
fontSize: 20,
fontColor: "Black"}
}]
}
}
And here is how I am adding the data with the borderdash:
function update_plot(json){
line_patter = [[1,1], [10, 10], [20, 5], [15, 3, 3, 3], [20, 3, 3, 3, 3, 3, 3, 3], [12, 3, 3]];
counter++;
chart1.data.datasets.unshift({
data: json.temp_pressure,
showLine: true,
pointRadius: 5,
fill: false,
label: "Plot " + counter,
borderDash: line_patter[counter % 6],
borderColor: getLineColor(chart1.data.datasets.length),
backgroundColor: getLineColor(chart1.data.datasets.length),
});
chart2.data.datasets.unshift({
data: json.fh2o,
showLine: true,
pointRadius: 5,
fill: false,
label: "Plot " + counter + ": H2O",
borderDash: line_patter[counter % 6],
borderColor: getLineColor(chart2.data.datasets.length),
backgroundColor: getLineColor(chart2.data.datasets.length),
});
chart2.data.datasets.unshift({
data: json.fo3,
showLine: true,
pointRadius: 5,
fill: false,
label: "Plot " + counter + ": O3",
borderDash: line_patter[counter % 6],
borderColor: getLineColor(chart2.data.datasets.length),
backgroundColor: getLineColor(chart2.data.datasets.length),
});
chart1.update();
chart2.update();
}
You need to provide your own Legend Label Configuration by defining legend.labels.generateLabels inside chart options.
In the following code sample, generateLabels creates an array of objects that implement the Legend Item Interface. Each object simply takes its properties from the corresponding dataset but leaves lineDash undefined.
new Chart(document.getElementById('myChartAxis'), {
type: 'line',
data: {
labels: ['A', 'B', 'C', 'D'],
datasets: [
{
label: 'WARNINGS',
data: [0, 2, 3, 2],
borderDash: [5],
borderColor: 'rgb(255, 159, 64)',
backgroundColor: 'rgba(255, 159, 64, 0.2)',
fill: false
},
{
label: 'ERRORS',
data: [1, 1, 4, 3],
borderDash: [2],
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgba(255, 99, 132, 0.2)',
fill: false
}
]
},
options: {
legend: {
labels: {
generateLabels: chart => chart.data.datasets.map((dataset, i) => ({
datasetIndex: i,
text: dataset.label,
fillStyle: dataset.backgroundColor,
strokeStyle: dataset.borderColor
}))
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js"></script>
<canvas id="myChartAxis" height="90"></canvas>

ChartJs - Angle line and ticks 'on top'

I have a chart as shown below. I have two expectation, firstly as you can see the tick number (1,2,3,4,5) hidden behind the background color of dump datasets (I need that background colors, that's why I am using fake datasets). I need to show them like using z-index or something. I wrote tick numbers in red color for sample.
Secondly I need the show angle line on top also.
var test2 = [2, 2, 2, 2, 2];
var test3 = [3, 3, 3, 3, 3];
var test4 = [4, 4, 4, 4, 4];
var test5 = [5, 5, 5, 5, 5];
new Chart(document.getElementById("myChart"), {
type: 'radar',
data: {
labels: sectionDescriptions,
datasets: [
{
label: "2050",
fill: true,
borderColor: "rgba(0,0,0,1)",
borderWidth: "4",
pointRadius: 3,
pointBorderWidth: 3,
pointBackgroundColor: "cornflowerblue",
pointBorderColor: "rgba(0,0,200,0.6)",
pointHoverRadius: 10,
data: sectionPoints
},
{
radius: 0,
fill: true,
backgroundColor: "rgba(240,0,0,1)",
data: test2
},
{
radius: 0,
fill: true,
backgroundColor: "rgba(255,190,4,1)",
data: test3
},
{
radius: 0,
fill: true,
backgroundColor: "rgba(255,250,5,1)",
data: test4
},
{
radius: 0,
fill: true,
backgroundColor: "rgba(0,180,233,1)",
data: test5
}
]
},
options: {
title: {
display: true,
text: 'Chart'
},
scale: {
gridLine: {
color : "white"
},
ticks: {
beginAtZero: true,
min: 0,
max: 5,
stepSize: 1,
fontColor: 'black',
backDropColor: 'black'
},
pointLabels: {
fontSize: 13,
//fontStyle: 'bold'
}
},
legend: {
display: false
}
}
});

Google charts - set max of one column

Is it possible to set a maxValue on one bar out of a "Material column charts"
For example over the series or axis.
I tried this:
vAxis: {
viewWindow: { max: [100] },
format: '000'
}
but it changes all columns
you would need to isolate the bar on a separate axis,
use the series option to assign, and vAxes for specific settings...
series: {
0: {
targetAxisIndex: 0
},
1: {
targetAxisIndex: 1
}
},
vAxes: {
0: {
viewWindow: {
max: 120
},
format: '000'
},
1: {
viewWindow: {
max: 100
},
format: '000'
}
}
see following working snippet...
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'y0');
data.addColumn('number', 'y1');
data.addRows([
[0, 96, 80.8],
[1, 97, 80.8],
[2, 98, 69.5],
[3, 99, 57],
[4, 100, 18.8],
[5, 101, 17.6],
[6, 102, 13.6],
[7, 103, 12.3],
[8, 104, 29.2],
[9, 105, 42.9]
]);
var options = {
height: 500,
series: {
0: {
targetAxisIndex: 0
},
1: {
targetAxisIndex: 1
}
},
vAxes: {
0: {
viewWindow: {
max: 120
},
format: '000'
},
1: {
viewWindow: {
max: 100
},
format: '000'
}
}
};
var chart = new google.charts.Line(document.getElementById('chart_div'));
chart.draw(data, google.charts.Line.convertOptions(options));
},
packages: ['line']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
note: must use google.charts.Line.convertOptions to get desired result...