score:0
I run your script, works fine with some enhancements in the way that each point shows the money but since the query returns 365 elements is too much data to show in the chart, that's why I think there are overlaying labels. I noticed that each value in the array has the same date (Sat Jan 17 1970 xx:yy:zz and Sun Jan 18 1970 qq:ww:ee) but different time.. then I group the data by these dates like this:
here's the entire script.
$('#getData').on('click', function(){
var requestURL = 'https://blockchain.info/de/charts/market-price?format=json&cors=true';
var request = new XMLHttpRequest();
let newData = [], labels = [];
let sameDate = "", previousDate = "", value = 0;
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onload = function() {
// Group the Data by Day
request.response.values.forEach(function(item,i){
if (previousDate !== getDateFormat(moment(item.x)._d) )
{
value = 0;
value = item.y;
sameDate = getDateFormat(moment(item.x)._d);
request.response.values.forEach(function(ele, j){
if ( j > i){
if (sameDate === getDateFormat(moment(ele.x)._d)){
value+= ele.y;
previousDate = getDateFormat(moment(ele.x)._d);
}
}
});
newData.push({x:sameDate, y:value});
labels.push(sameDate);
}
});
drawChart(newData,labels);
}
function getDateFormat(momentType)
{
console.log(momentType);
return momentType.getDate()+"-"+(momentType.getMonth()+1)+"-"+momentType.getUTCFullYear();
}
function drawChart(jsonObj,label) {
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: label,
datasets: [{
label: "Average USD",
data:jsonObj}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}],
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'value'
}
}]
},
plugins: {
datalabels:{
borderRadius: 4,
color: 'black',
anchor: 'end',
align: 'end',
backgroundColor: function(context) {
return context.dataset.borderColor;
},
formatter: function(value, context){
// show the value on the point
return Number(value.y).toFixed(2);
},
}
}
}
});
}
});
score:0
In addition, here's my entire script.
var requestURL = 'https://blockchain.info/de/charts/market-price?format=json&cors=true';
var request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onload = function() {
var response = request.response;
drawChart(response);
}
function fillLabel(jsonObj){
var label = [];
var arr = jsonObj["values"];
arr.forEach(element => {
label.push((element.x));
});
return label;
}
function drawChart(jsonObj) {
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: fillLabel(jsonObj),
datasets: [{
label: jsonObj["name"],
data: jsonObj["values"],
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}],
xAxes: [{
type: 'time',
time: {
unit: 'second',
displayFormats: {
millisecond: 'h:mm:ss.SSS a',
second: 'D MMM',
minute: 'D MMM',
hour: 'hA',
day: 'MMM D',
week: 'll',
month: 'MMM YYYY',
quarter: '[Q]Q - YYYY',
year: 'YYYY'
},
},
display: true,
scaleLabel: {
display: true,
labelString: 'value'
}
}]
}
}
});
}
score:1
In the image above you can see the data, notice that I separate it in three arrays, in the chart I have two datasets (Ingreso(x[date],y[value]), Egreso(x[date],y[vlue])) and "Label" that is the one that fills the x-Axis label. And here is how I did it !
so in the label option I fill with my label array with the dates
Source: stackoverflow.com
Related Query
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- how to label axis within radar chart with chart.js
- How to stop axis label from being cut off in chart JS?
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- Chart Js change text label orientation on Ox axis
- show label in tooltip but not in x axis for chartjs line chart
- How to wrap X axis labels to multi-lines (X axis label formatting) in ng2-Charts?
- How do I change the 'months' language displayed on the date axis in Chart JS?
- PrimeNg bar chart how to show a label for the y-axis
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- How to add label in chart.js for polar chart area
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- How to Remove axis Lines from chart in chart js
- how to minimize x axis labels to day hours in chart js
- display vertical axis label in line chart using chart.js
- chart.js: How do I make the radar chart axis labels bigger?
- Chart Js reduce text size for label on X axis
- How to change default label of each bubble in bubble chart using chartjs-plugin-datalabels
- How to rotate the Label text in doughnut chart slice vertically in chart.js canvas, Angular 12?
- Chart JS, ng2-Charts - How to make the label to the right of pie chart instead on top?
- How to set custom Y Axis Values (Chart.js v2.8.0) in Chart Bar JS
- Chart.js how to remove final label on chart
- How to apply dollar sign in Y- axis in chart js?
- Chart.js - how to make proportional intervals on X axis on line chart
- How to introduce newline character in xAxes Label with chart js
- How to detect click on chart js 3.7.1 axis label?
- How to increase space between label and chart area in chart.js
- How to scale label size radar chart chart.js
More Query from same tag
- How to change font size, padding of chart title only, in chart.js 3.x
- Chart in Angular not displayed until windows resize
- Charts.JS - Too Many Charts? - Loading Issue
- How to Display Chart.js line-chart from a MVC Controller
- display various chart value and tool tip
- ChartJS - format datetime X axis when displaying data from MySql table
- Update a chartjs by javascript function
- I am not able to show dotted circle for percentage
- Set Global Config on Angular Charts Not Working
- Update the chart data from an array stored in a variable on button click
- Chart changes different on zoom when using multiple charts with chart.js and flask
- Manipulate chart.js based plots in R (radarchart)
- Remove Angularjs-Chart border and reduce chart arc thickness
- Inverting X axis number labels in chart.js scatter chart
- ChartJS - Line Chart with different size datasets
- ChartJS show jittering on hover
- how to create a bar chart with php data variables
- chart.js uncaught reference error while updating
- How can I create custom tooltips for each data point in a graph?
- how to render the same chart using Chart.js
- Trouble using utcoffset with Chart.js
- How to Save as png with ChartJSRadar in R?
- How to eliminate all borders from chartjs2 stacked chart
- Chart.js click on labels, using bar chart
- Chartjs - line chart display with addData() is bugged?
- Hover over chart shows multiple datapoints
- Add labels to bar chart: chartjs
- Chart.js Cannot read property 'apply' of undefined
- How to make dynamic chart using Vue component with chart-js
- Assigning a value to an object w/bracket notation