Prevent Area plot Y-axis from starting with ZERO - javascript
What is the configuration for Y-axis to not start with ZERO but rather start with value close to lowest Y-axis data point in HighCharts/HighStock?
Here is the demo of chart which has a very great Y-axis data values while the minimal Y-axis plot point is ZERO: http://jsfiddle.net/ubnjjups/
and sample configuration used in the demo:
{
series: [{
type: 'area',
data: [
[1375660800000, 106861],
[1375747200000, 107397],
[1375833600000, 108674],
[1375920000000, 108792],
[1376006400000, 110504],
[1376265600000, 110658],
[1376352000000, 110792]
]
}],
}
The problem here is that you are using 'area' type which by definition needs to show you area from 0 to the point. If you switch to say 'spline', it automatically does what you want.
$(function () {
$('#container').highcharts('StockChart', {
series: [{
type: 'spline',
data: [[1375660800000, 106861],[1375747200000, 107397],[1375833600000, 108674],[1375920000000, 108792],[1376006400000, 110504],[1376265600000, 110658],[1376352000000, 110792],[1376438400000, 111095],[1376524800000, 112334],[1376611200000, 112775],[1376870400000, 113051],[1376956800000, 113426],[1377043200000, 113516]]
}],
});
});
UPDATE:
If you want to continue to use Area chart (so you have filled area under), I recommend you use min on your yAxis. I have changed your code here
$(function () {
$('#container').highcharts('StockChart', {
series: [{
type: 'area',
data: [[1375660800000, 106861],[1375747200000, 107397],[1375833600000, 108674],[1375920000000, 108792],[1376006400000, 110504],[1376265600000, 110658],[1376352000000, 110792],[1376438400000, 111095],[1376524800000, 112334],[1376611200000, 112775],[1376870400000, 113051],[1376956800000, 113426],[1377043200000, 113516]]
}],
yAxis:{
min: 105000
}
});
});
The value of min can be calculated ahead of time when you get your data back from the server and calculate your minimum value point. Hope this helps.
Based on #Vadim's answer, as of today I suggest calculating lowest Y-axis value inside the yAxis.min configuration itself to keep code consistent and portable. The only requirement is to extract the data into a variable outside the chart initialization.
With data in format <Timestamp>, <Y-axis value> following would work:
var data = [
[1375660800000, 106861],
[1375747200000, 107397],
[1375833600000, 108674]
];
$('#container').highcharts('StockChart', {
series: [{
type: 'area',
data: data
}],
yAxis: {
min: (function() {
var min = data[0][1];
for (var i = 0; i < data.length; i++) {
var value = data[i][1];
if (value < min) {
min = value;
}
}
return min;
})()
}
});
Live demo: http://jsfiddle.net/squuwqmg/
Related
Highchart with float data type and with max value on Y-axis
I want to to plot high chart with float data type on Y-axis but with sql server's max float limit. I am not able to plot the chart using maximum float value. For reference: http://jsfiddle.net/e3ary1fj/2/ $(function () { $('#container').highcharts({ yAxis: { labels: { formatter: function(){ return this.value.toExponential(3).replace('e-','E-0'); } } }, series: [{ data: [1e-8,0.355e-8] }] }); }); Please manipulate data like following in above mentioned link: series: [{ data: [1e-8,1.79E+308] }] When float has big exponential value like sql server's maximum value then I am not able to plot the highchart. Please try following to reproduce this issue: $(function () { $('#container').highcharts({ yAxis: { labels: { formatter: function(){ return this.value.toExponential(3).replace('e-','E-0'); } } }, series: [{ data: [1e-8,1.79E+308] }] }); }); Thanks, Dhirendra
Price history with chart js
I have this kind of data array: [ { "price":"49", "date":"21\/01\/2018" }, { "price":"30", "date":"01\/01\/2018" }, { "price":"32", "date":"15\/11\/2017" } ] Now I want to create a chart with chartjs, that shows me a price curve for the last 12 month. I wrote this little script to generate me the past months: function getPreviousMonths() { var months = []; for (i = 0; i < 12; i++) { var month = moment().subtract(i, 'months').format('MMMM Y'); months.push(month); } return months.reverse(); } How can I create the chartjs chart now? I looked in the docs, but got very confused when it comes to set dates within axes...
See http://www.chartjs.org/docs/latest/axes/cartesian/time.html for setting time scale on xAxes, then you have to convert your date field to a real date object: xAxes: [{ type: 'time', distribution: 'linear', ticks: { source: 'labels' }, time: { unit: 'month', unitStepSize: 1, displayFormats: { 'month': 'MMM' } } } Check this jsfiddle showing an example of time serie rendered as a line: https://jsfiddle.net/beaver71/9f9a2z88/
You have 2 separate your data array into 2 different arrays. One of dates (say dates_array) and another of price (say price_array). Then you just have to create new chart. var chart = new Chart(element, { type: 'line', data: { labels: dates_array, datasets: [{ label: '# price', data: price_array }] } }); Here, element is the element in which chart will be shown. labels will be assigned the date array and data will be assigned price array. You can check this jsfiddle https://jsfiddle.net/j7gta8yn/
How to properly feed data to ChartJS with different number of x(labels) and y(data) points
I have a dataset that has data something like this var data =[10,30,20,50,80,60,120,40,20,90,30,10]; var labels = [moment("12:00:00", 'HH:mm:ss'),moment("12:00:01", 'HH:mm:ss'),moment("12:00:02", 'HH:mm:ss'),moment("12:00:03", 'HH:mm:ss')]; I fed the data to chartJS like this var ctx = document.getElementById("myChart").getContext('2d'); var myChart = new Chart(ctx, { type: 'line', data: { labels: labels, datasets: [{ label: 'Voltage Fluctuation', data: [10,20,30,40,50], borderWidth: 1 }] }, options: { scales: { xAxes: [{ type: 'time', time: { unit: 'minute', displayFormats: { hour: 'HH:mm:ss' } } }] }, } }); However, I'm only getting data for the first four points i.e for each label. Here's the JSFiddle I want the data to be distributed for all the labels, in this case one data point for every (4/12)seconds and adjust the graph accordingly. Is there any possible way I can achieve that without hardcoding it by converting the labels to milliseconds format?
I went ahead and hardcoded the entire thing by chopping seconds into milliseconds in order to create arrays of equal length
Highcharts: how to define x-axis for dynamic chart
I am using Highcharts to automatically plot an array which is output from a sensor and is updated every second or so. The array is x elements long. This particular sensor measures the light intensity at x steps between some min and max wavelengths (that is, it measures the intensity at increments of (max-min)/x--in my case this increment is not an integer). I also have a static data set that is plotted on the same axes with the same x-axis requirements. I am able to successfully graph this data dynamically, but the x-axis scale is wrong. Instead of ranging from min to max, Highcharts defaults to a range of min to min+x. I'd like to change the x-axis increment so that the scaling is correct. Can a min, max, and number of data points or step be defined to generate the x-axis? Or is there a way to define the x- and y-axis values as individual arrays that are plotted against each other? Some other way? I have done lots of searching and experimenting but have come up short. The relevant snippet of my code is below. function showData(result) { // 'result' is an array that comes from the sensor var numbers = int(split(resultString, ",")); chart.series[1].setData(numbers); socket.send('a'); // send a byte to tell it to start sending new data loop++; //increase loop every time the server receives data chart.setTitle({ text: 'Spectrum, reading #' + loop }); } //showData $(function () { chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'area', load: function() { chart = this; showData(); } }, title: { text: 'Spectrum, reading 0' }, xAxis: { title: { text: 'Wavelength [nm]', allowDecimals: false, }, }, yAxis: { title: { text: '' }, }, tooltip: { pointFormat: '{point.x}' }, plotOptions: { area: { pointStart: 340, marker: { enabled: false, symbol: 'circle', radius: 2, states: { hover: { enabled: true } } } } }, series: [{ name: '404 nm laser spectrum', data: [130,130,114,113,113,116,112,111,112,112,115,113,113,115, 112,114,113,113,114,115,113,114,113,114,115,115,117,119,124, 136,145,164,190,217,252,363,482,491,417,285,188,156,140,132, 127,122,117,118,117,115,116,115,116,118,116,116,117,116,117, 116,113,117,114,113,115,112,116,114,114,116,114,114,116,113, 116,115,114,115,115,114,115,115,115,116,114,115,116,114,118, 114,116,116,115,118,114,113,117,113,116,116,115,116,115,115, 115,114,117,116,117,118,120,118,122,119,128,127,130,134,136, 138,140,137,139,134,136,134,132,133,134,131,132,130,130,131, 128,128,131,129,131,131,134,136,134,140,139,137,143,140,138, 141,136,134,132,127,126,126,123,123,118,119,122,118,120,117, 116,118,116,118,116,115,117,116,115,116,115,115,116,114,119, 113,114,116,115,116,114,114,116,116,113,117,116,114,118,112, 115,114,113,116,115,114,115,113,116,114,114,116,115,115,114, 112,114,114,113,114,115,113,117,114,115,112,114,114,113,115, 114,114,115,113,112,115,112,113,115,112,116,113,113,115,116, 113,116,113,115,113,114,115,115,114,116,114,116,113,116,117, 113,115,116,115,117,115,114,117,113,115,118,114,116,115,115, 116,114,113,116,114,117,115,114,117,115,114,115,116,116,116, 117,117,114,0], color: '#36D39F' }, { name: 'Current measured spectrum', data: numbers, color: '#4A235A' }] }); }); EDIT: here's a demo showing how mine currently functions: https://jsfiddle.net/bgzgc1d9/2/. The x-axis should range from 340 to 850 with 288 data points evenly spaced on this interval
Highchart xAxis label steps wrong
I am loading Highcharts like this. var options = { credits: { enabled: false }, chart: { renderTo: 'chart_box', type: 'areaspline' }, title: { text: '' }, xAxis: { crosshairs: true, labels: { step: 5, rotation: -45 } }, series: [] }; Then I have a function which is called when graph needs to be loaded. Upon calling the function, data is fetched through AJAX and assigned to series and date lie this: $.ajax({ url: 'url/charts', type: 'post', data: data }).done(function(data) { var dateCount = data.dates.length; var stepCount = 1; if (dateCount > 10) { stepCount = 5; } options.xAxis.categories = data.dates; $.each(data.series, function(name, elem) { options.series.push({ name: name.replace('_', ' ').toUpperCase().trim(), data: elem }) }); chart = new Highcharts.Chart(options); }); The issue here is that even though I have given step as 5 , it is showing dates with 15 dates interval. I mean in xAxis labels. It seems like it will be multiplied by three always. If I give 2, it will show 6 days interval in labels. Everything working fine in a chart which is not using AJAX to load data.