Chart.js: Don't stretch axes beyond chart - javascript

I'm using chart.js 2.9.4 and the ng2-charts wrapper for Angular. I'm trying to display incoming live data, but have trouble configuring the chart so that the ticks/axis don't stretch beyond the data in the chart. In other words, I want the chart data points to fill the entire width of the chart grid.
StackBlitz showing my issue.
If you keep looking at the chart as data is added, you see that most of the time the ticks stretch beyond the last point in the chart:
The only solution I could come up with is overwriting the max value of the ticks on the x-axis each time new data is added to the chart: options.scales.xAxes[0].ticks.max = x;. Uncomment line 68 in the StackBlitz in order to apply this. This solves my problem but introduces another. Sometimes as data gets added, the last two ticks overlap:
I've tried experimenting with various parameters of the scales and ticks options (bounds,distribution,stepSize,source,autoSkip,autoSkipPadding) with no results. Is there any combination of configuration parameters to solve my issue ?

You may want to add delay at streaming plugin, the config will look something like:
scales: {
x: {
type: 'realtime', // x axis will auto-scroll from right to left
realtime: { // per-axis options
delay: 1000, // delay of 1000 ms, so upcoming values are known before plotting a line
pause: false, // chart is not paused
ttl: undefined, // data will be automatically deleted as it disappears off the chart
}
},
I have tested this on your code, and it seems to work as expected, you may want to tune some parameters as needed.

Related

Reverse one chart and leave the other unreversed - Synchronized highcharts

In order to reverse a chart you have to add reversed: true. It can be done either on the xAxis or the yAxis. In this example provided by Highcharts: https://jsfiddle.net/omaraziz/zg516ruk/ when I add reversed, it reverses all the charts. How can I reverse only one chart? More generally, what is the proper way to do more customization on one chart and not the other when the two are synchronized?
I chose synchronized because I wanted the values from both to be displayed at the same time when scanning from right to left and vice versa.
For now every time I change the legend position, reverse xAxis or yAxis, play with the tooltip, set a max and min for the yAxis, etc. it does it to both.
Here's my JSON data, in case the way to do it is through the JSON file: https://omaraziz.me/CC-chart/activity.json
You can use index variable i to recognize the chart. For example the IFFE function below return true only for the second chart:
xAxis: {
reversed: (function() {
return i === 1
})(),
...
}
Live demo: https://jsfiddle.net/BlackLabel/7fx6jdnw/

Highcharts Y-Axis Limits

I am having problems controlling the Y-Axis range of a highcharts graph. It seems like highcharts likes nice round numbers. When my data passes or is close to certain thresholds, the Y-Axis range can expand a lot which effectively compresses all the plot points downward.
Here is a jsfiddle that illustrates the problem I am having:
https://jsfiddle.net/shannonwrege/z8h5eork
The relevant code for this post is this:
chart.yAxis[0].setExtremes(0, max, true, false);
Keep in mind that I don't know what the data will look like in advance, so I must dynamically modify the Y-Axis range. Right now I am using the setExtremes because of other suggestions I've read on stackoverflow.
The maximum y-value of the data in the first two charts is 99. You'll notice that the y-axis is set at 150 in the first chart where the range is automatically calculated and 100 in the second chart where I specifically set the extreme values. The look of the 2nd chart is what I want. So it seems like setExtremes(0,99,true,false) should do the trick, but it actually doesn't.
In the 3rd chart I changed the data so that the maximum y-value of the data is 101, and I called setExtremes(0,101,true,false). You'll note that the y-axis is now back to 150.
Ideally I want the scale of the graph to be capped on the maximum value to limit the about of extra white space. I want to see all of the data, but I don't necessarily care about the y-axis displaying a maximum band that is greater than the max data value. In this case, I would be happy with the y-axis displaying 100 on the axis and some points over but still visible.
Does anyone know how to make this work?
I ended up using the endOnTick parameter to solve this problem. Adding the following line to the yAxis configuration parameters did exactly what I wanted:
endOnTick: false,
Here's the updated Fiddle showing the results.
https://jsfiddle.net/shannonwrege/z8h5eork/3/
All of the charts look pretty good in my opinion (even the one where the yAxis range was auto calculated).
You will need to read the data and then round up to set the idealMax
var chart,
idealMax = 0; // init the max value
// Read the data to find the highest value
for (i=0;i < (options.series[0].data).length; i++ ){
if (options.series[0].data[i][1] > idealMax) {
idealMax = options.series[0].data[i][1];
}
}
// Round the max to the nearest 10
idealMax = Math.round(idealMax / 10) * 10;
options.yAxis.tickPixelInterval = idealMax/10;
Highcharts.chart('container1', options);
chart = $('#container1').highcharts();
chart.yAxis[0].setExtremes(0, idealMax, true, false);
Updated Fiddle

Highstock/Highchart scatter points in combined graph

I am trying to plot a combination chart of a scatter and columnrange.
I am having issues with the scatter plot not showing correctly. The points seem to "stop" at a certain point after zooming in and scrolling.
https://codepen.io/moosejaw/pen/QavGgR?editors=0011
It must be related to the boost module?
I've tried enabling and disabling it.
boost: {
// enabled: false // works
enabled: true // doesn't work
}
I know the boost module can handle the number of points, but I am confused on why their is a scrolling / placement issue.
Thanks for your time.
This issue happens only when the chart is inverted.
It's a bug reported on github: https://github.com/highcharts/highcharts/issues/4608
The workaround here is to create inverted chart manually (which can be done via chart options and preprocessing the data):
Swap x and y values for all points
Set reversed to true for the y axis (when chart is inverted it's set by default: https://api.highcharts.com/highcharts/chart.inverted)
Swap formatting and zoom options for axes
Handle the format of tooltip (tooltip.formatter may be useful)

D3JS How to Adapt Scatterplot to Show "semi-continuous" X-Axis Data

This question is kind of conceptual, so if this isn't the right place for this, I apologize.
I have an existing scatterplot that has continuous Y-axis data and used to have continuous X-axis data. My X-axis data has now changed to semi-continuous, it's a set series of number (cost values), like [200, 450, 600, 900, 1400]. There are about 250 'points' on the scatter-plot, but now it seems strange because the X values are not continuous, and they create vertical columns of points.
Without creating an entirely new graph, is there something I can add or do to these data points to better represent them? I was thinking something along the lines of a box-whisker, but a box-whisker wouldn't work for these values.
Edit Question Clarification:
Is there a type of chart or adjustment that I can make to this chart to better display these values as unique, interesting points of data?

How to get x-axis start value in Flot?

I would like to know how to get x-axis start value in JS library Flot. I know x-axis value in the start (obviously, because I fill it with my own data), but if you set:
pan: {
interactive: true
}
Than the user is able to pan through graph (example here) and if the user pans through graph, the x-values change. I looked inside jquery.flot.js to find where this values are computed but with no luck.
I got it. The solution was in front of me the whole time (example):
var axes = plot.getAxes();
var min = axes.xaxis.min;

Categories

Resources