Google LineChart Displaying Nagative Value - charts

I am displaying Google Line chart using below code
google.charts.load('current', {'packages':['corechart']});
//google.charts.setOnLoadCallback(drawChart);
function drawChart(chart_data) {
var jsonData = chart_data;
var data = new google.visualization.DataTable();
data.addColumn('number', 'Day');
data.addColumn('number', 'SOLD');
data.addColumn('number', 'BOUGHT');
$.each(jsonData, function(i, jsonData){
var Day = parseFloat($.trim(jsonData.Day));
var SOLD = parseFloat($.trim(jsonData.SOLD));
var BOUGHT = parseFloat($.trim(jsonData.BOUGHT));
data.addRows([[Day, SOLD, BOUGHT]]);
});
var options = {
fontName: 'Roboto',
height: 400,
fontSize: 12,
chartArea: {
left: '7%',
width: '93%',
height: 350
},
pointSize: 10,
curveType: 'function',
backgroundColor: 'transparent',
tooltip: {
textStyle: {
fontName: 'Roboto',
fontSize: 13
}
},
vAxis: {
title: '',
titleTextStyle: {
fontSize: 13,
italic: false,
color: '#333'
},
viewWindow: {
min: 0
},
textStyle: {
color: '#333'
},
baselineColor: '#ccc',
gridlines:{
color: '#eee',
count: 10
}
},
hAxis: {
textStyle: {
color: '#333'
}
},
legend: {
position: 'top',
alignment: 'center',
textStyle: {
color: '#333'
}
},
series: {
0: { color: '#f58646' },
1: { color: '#66BB6A' }
}
};
var chart = new google.visualization.LineChart(document.getElementById('google-line'));
chart.draw(data, options);
}
Its working fine like below if there data greater than 0
But if there all data value is 0, its display like below
I do not want display negative data and I will have never negative value in my data. So I have added code like below in options as suggested in some questions here.
viewWindow: {
min: 0
},
However its not working and still I have negative values showing. Can anyone help me here for solve the puzzle? Also in horizontal line its missing dates and show in batch of 5 like 5, 10, 15, its possible to show all?
Thanks!

that looks like some sort of bug with the 'current' version.
if we use version '45' it works properly.
google.charts.load('45', ...
see following working snippet...
google.charts.load('45', {
packages: ['corechart']
}).then(function() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Day');
data.addColumn('number', 'SOLD');
data.addColumn('number', 'BOUGHT');
data.addRows([
[0, 0, 0],
[5, 0, 0],
[10, 0, 0],
[15, 0, 0],
[20, 0, 0],
[25, 0, 0],
[30, 0, 0]
]);
var options = {
fontName: 'Roboto',
height: 400,
fontSize: 12,
chartArea: {
left: '7%',
width: '93%',
height: 350
},
pointSize: 10,
curveType: 'function',
backgroundColor: 'transparent',
tooltip: {
textStyle: {
fontName: 'Roboto',
fontSize: 13
}
},
vAxis: {
title: '',
titleTextStyle: {
fontSize: 13,
italic: false,
color: '#333'
},
viewWindow: {
min: 0
},
textStyle: {
color: '#333'
},
baselineColor: '#ccc',
gridlines: {
color: '#eee',
count: 10
}
},
hAxis: {
textStyle: {
color: '#333'
}
},
legend: {
position: 'top',
alignment: 'center',
textStyle: {
color: '#333'
}
},
series: {
0: {
color: '#f58646'
},
1: {
color: '#66BB6A'
}
}
};
var chart = new google.visualization.LineChart(document.getElementById('google-line'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="google-line"></div>
or, when using the 'current' version,
if we add a max value to the viewWindow, then it will work...
google.charts.load('current', {
packages: ['corechart']
}).then(function() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Day');
data.addColumn('number', 'SOLD');
data.addColumn('number', 'BOUGHT');
data.addRows([
[0, 0, 0],
[5, 0, 0],
[10, 0, 0],
[15, 0, 0],
[20, 0, 0],
[25, 0, 0],
[30, 0, 0]
]);
var options = {
fontName: 'Roboto',
height: 400,
fontSize: 12,
chartArea: {
left: '7%',
width: '93%',
height: 350
},
pointSize: 10,
curveType: 'function',
backgroundColor: 'transparent',
tooltip: {
textStyle: {
fontName: 'Roboto',
fontSize: 13
}
},
vAxis: {
title: '',
titleTextStyle: {
fontSize: 13,
italic: false,
color: '#333'
},
viewWindow: {
min: 0,
max: 1
},
textStyle: {
color: '#333'
},
baselineColor: '#ccc',
gridlines: {
color: '#eee',
count: 10
}
},
hAxis: {
textStyle: {
color: '#333'
}
},
legend: {
position: 'top',
alignment: 'center',
textStyle: {
color: '#333'
}
},
series: {
0: {
color: '#f58646'
},
1: {
color: '#66BB6A'
}
}
};
var chart = new google.visualization.LineChart(document.getElementById('google-line'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="google-line"></div>

Related

Customized label and Emphasis label overlap

I try to make the pie chart show 'customized label',change label when the mouse triggered. But except blue data everything else will overlap with 'emphasis label'.
'avoidLabelOverlap' not working.
Is there way?
enter image description here
enter image description here
this is a example
var option = {
tooltip: {
trigger: 'item'
},
legend: {
top: '5%',
left: 'center'
},
series: [
{
name: 'Access From',
type: 'pie',
radius: ['40%', '70%'],
avoidLabelOverlap: false,
itemStyle: {
borderRadius: 10,
borderColor: '#fff',
borderWidth: 2
},
label: {
show: true,
position: 'center',
formatter: function (params) {
var html = 'TEST';
return html;
}
},
emphasis: {
label: {
show: true,
fontSize: '40',
fontWeight: 'bold'
}
},
labelLine: {
show: false
},
data: [
{ value: 1048, name: 'Search Engine' },
{ value: 735, name: 'Direct' },
{ value: 580, name: 'Email' },
{ value: 484, name: 'Union Ads' },
{ value: 300, name: 'Video Ads' }
]
}
]
};
Finally,I find a method.
Use mouse event to change label in center.
<!DOCTYPE html>
<html lang="Zh-TW">
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.2.2/echarts.min.js"></script>
<meta charset="utf-8">
</head>
<body>
<div id="main" style="width:600px;height:600px;">
</div>
<script>
var myChart = echarts.init(document.getElementById('main'));
var option = {
title: {
text: 'Total',
textStyle: {
fontSize: 35,
},
x: 'center',
y: 'center'
},
tooltip: {
trigger: 'item'
},
legend: {
top: '5%',
left: 'center'
},
series: [
{
name: 'Testname',
type: 'pie',
radius: ['40%', '60%'],
center: ['50%', '50%'],
avoidLabelOverlap: true,
textAlign: 'center',
label: {
normal: {
show: true,
formatter: function (params) {
return params.name+'\n'+params.percent+'%'
},
textStyle: {
fontSize: 20
},
},
},
itemStyle: {
borderRadius: 10,
borderColor: '#fff',
borderWidth: 2
},
data: [
{ value: 1048, name: 'Search Engine' },
{ value: 735, name: 'Direct' },
{ value: 580, name: 'Email' },
{ value: 484, name: 'Union Ads' },
{ value: 300, name: 'Video Ads' }
]
},
{
name: 'Testname',
type: 'pie',
radius: ['40%', '60%'],
center: ['50%', '50%'],
avoidLabelOverlap: true,
textAlign:'center',
label: {
normal: {
show: true,
position: 'center',
formatter: '{b}\n{c}',
align: 'center',
verticalAlign: 'middle',
textStyle: {
fontSize: 0
},
},
emphasis:{
show: true,
textStyle: {
fontSize: 35,
fontWeight: 'bold'
},
formatter: '{b}\n{c}'
},
},
itemStyle: {
borderRadius: 10,
borderColor: '#fff',
borderWidth: 2
},
data: [
{ value: 1048, name: 'Search Engine' },
{ value: 735, name: 'Direct' },
{ value: 580, name: 'Email' },
{ value: 484, name: 'Union Ads' },
{ value: 300, name: 'Video Ads' }
]
},
]
};
myChart.setOption(option);
myChart.on('mouseover', { seriesName: 'Testname' }, () => {
myChart.setOption({
title: {
show: false
}
})
})
myChart.on('mouseout', { seriesName: 'Testname' }, () => {
myChart.setOption({
title: {
show: true
}
})
})
</script>
</body>
</html>
Thanks for the accepted answer,
My case was series label was showing like you mentioned in question, and my need was to solve the same problem while hovering the mouse through legend.
Here is my addition.
// series name: Access From
labelOptionAlter = {
series: [
{ label: {
show: false,
position: 'center'
},
}
]
};
labelOptionDefault={
series: [
{ label: {
show: true,
position: 'center'
},
}
]
};
//similar to mouseover.
myChart.on('highlight', { seriesName: 'Access From' }, () => {
myChart.setOption(labelOptionAlter)
})
//similar to mouseout
myChart.on('downplay', { seriesName: 'Access From' }, () => {
myChart.setOption(labelOptionDefault)
})
Future scholars can read more here
I think there's no way to solve this problem.

Laravel 8 ConsoleTvs / Charts 7 Implementing Chartjs Options

I am currently updating ConsoleTvs / Charts 6 to 7. The chart is rendering with expected data.
The chart is not rendering according to any of the set options object keys for legend point style circle, padding top 50, display 'y' & 'x' axis labelString, not display gridlines, positioning datalabels at end with start offset in bold and green :
const chart = new Chartisan({
el: '#test_chart',
url: "#chart('test_chart_route')",
hooks: new ChartisanHooks()
.colors()
.datasets(
[
{ type: 'line', fill: false, borderColor: '#329865', backgroundColor: '#329865' },
{ type: 'line', fill: false, borderColor: '#1e5b3c', backgroundColor: '#1e5b3c' },
{ type: 'line', fill: false, borderColor: '#0f2d1e', backgroundColor: '#0f2d1e' },
{ type: 'line', fill: false, borderColor: '#329865', backgroundColor: '#329865' },
{ type: 'line', fill: false, borderColor: '#1e5b3c', backgroundColor: '#1e5b3c' },
{ type: 'line', fill: false, borderColor: '#0f2d1e', backgroundColor: '#0f2d1e' },
{ type: 'line', fill: false, borderColor: '#800000', backgroundColor: '#800000' },
{ type: 'line', fill: false, borderColor: '#F70000', backgroundColor: '#F70000' },
{ type: 'line', fill: false, borderColor: '#FF6F6F', backgroundColor: '#FF6F6F' },
{ type: 'line', fill: false, borderColor: '#800000', backgroundColor: '#800000' },
{ type: 'line', fill: false, borderColor: '#F70000', backgroundColor: '#F70000' },
{ type: 'line', fill: false, borderColor: '#FF6F6F', backgroundColor: '#FF6F6F' },
]
),
options: {
layout: {
padding: {
left: 0,
right: 0,
top: 50,
bottom: 0
},
},
aspectRatio: 1,
maintainAspectRatio: false,
responsive: true,
legend: {
display: true,
position: 'top',
labels: {
usePointStyle: true,
fontSize: 12,
},
},
elements: {
point: {
pointStyle: 'circle',
}
},
scales: {
xAxes: [{
maxBarThickness: 120,
scaleLabel: {
display: true,
labelString: "xAxes_label"
},
gridLines: {
display: false
},
ticks: {
fontSize: 10,
maxRotation: 80,
minRotation: 80,
padding: 2
},
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: "yAxes_label"
},
gridLines: {
display: false,
drawBorder: false
},
ticks: {
display: true,
fontSize: 10,
suggestedMin: 0
},
}],
},
plugins: {
datalabels: {
color: '#ff0a6c',
labels: {
title: {
font: {
weight: 'bold',
size: 11,
}
},
value: {
color: 'green'
}
},
formatter: function(value, context) {
return (value != '' && value !== undefined) ? Math.round(value * 100) / 100 : value;
},
anchor: 'end',
align: 'start',
display: 'auto',
clamp: false
}
}
}
});
I have also tried using the ChartisanHooks().options({...options...}) method with no change in set options.
Can anyone please show me in the right direction?
Instead of using the options hook, you defined options as a property. You should use .options({ ... }) but not options: { ... }.
The options hook however doesn't work for me, maybe I use an outdated Chartisan library. According to Custom Hooks, you can also merge the options to the chart as follows:
.custom(function({ data, merge }) {
return merge(data, {
options: {
...
}
}),
})
Please take a look at your amended and runnable code below and see how it works with the merge function.
const chart = new Chartisan({
el: '#test_chart',
data: {
"chart": {
"labels": ["First", "Second", "Third"]
},
"datasets": [{
"name": "Sample 1",
"values": [10, 3, 7]
},
{
"name": "Sample 2",
"values": [1, 6, 2]
}
]
},
hooks: new ChartisanHooks()
.colors()
.datasets(
[{
type: 'line',
fill: false,
borderColor: '#329865',
backgroundColor: '#329865'
},
{
type: 'line',
fill: false,
borderColor: '#1e5b3c',
backgroundColor: '#1e5b3c'
}
]
)
.custom(function({ data, merge }) {
return merge(data, {
options: {
layout: {
padding: {
left: 0,
right: 0,
top: 50,
bottom: 0
},
},
aspectRatio: 1,
maintainAspectRatio: false,
responsive: true,
legend: {
display: true,
position: 'top',
labels: {
usePointStyle: true,
fontSize: 12,
},
},
elements: {
point: {
pointStyle: 'circle',
}
},
scales: {
xAxes: [{
maxBarThickness: 120,
scaleLabel: {
display: true,
labelString: "xAxes_label"
},
gridLines: {
display: false
},
ticks: {
fontSize: 10,
maxRotation: 80,
minRotation: 80,
padding: 2
},
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: "yAxes_label"
},
gridLines: {
display: false,
drawBorder: false
},
ticks: {
display: true,
fontSize: 10,
suggestedMin: 0
},
}],
},
plugins: {
datalabels: {
color: '#ff0a6c',
labels: {
title: {
font: {
weight: 'bold',
size: 11,
}
},
value: {
color: 'green'
}
},
formatter: function(value, context) {
return (value != '' && value !== undefined) ? Math.round(value * 100) / 100 : value;
},
anchor: 'end',
align: 'start',
display: 'auto',
clamp: false
}
}
}
});
}),
});
<script src="https://unpkg.com/chart.js#2.9.4/dist/Chart.min.js"></script>
<script src="https://unpkg.com/#chartisan/chartjs#^2.1.0/dist/chartisan_chartjs.umd.js"></script>
<div id="test_chart" style="height: 300px;"></div>

Is it possible to make a double stack chart in ECharts

The ECharts provide stack option to stack multiple area/line charts together.
https://echarts.apache.org/en/option.html#series-line.stack
I have three charts that look like this:
To achieve it you can paste the following code here: https://echarts.apache.org/examples/en/editor.html?c=area-stack
option = {
title: {
text: 'Double stack',
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985',
},
},
},
legend: {
data: ['stack 1', 'stack 2', 'basis'],
},
toolbox: {
feature: {
saveAsImage: {},
},
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true,
},
xAxis: [
{
type: 'category',
boundaryGap: false,
data: ['0', '1', '2', '3', '4', '5', '6'],
},
],
yAxis: [
{
type: 'value',
},
],
series: [
{
name: 'stack 1',
type: 'line',
areaStyle: {
color: 'red',
},
data: [140, 150, 160, 180, 160, 240, 160],
},
{
name: 'stack 2',
type: 'line',
areaStyle: {
color: 'green',
},
data: [120, 140, 130, 150, 120, 160, 125],
},
{
name: 'basis',
type: 'line',
areaStyle: {
color: 'blue',
},
data: [100, 110, 120, 130, 90, 130, 120],
},
],
};
However I would like to stack both the green and the red charts on the blue one to get something like this:
I can add the values of the blue one to both other charts and get the result and get the following options:
option = {
title: {
text: 'Double stack',
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985',
},
},
},
legend: {
data: ['stack 1', 'stack 2', 'basis'],
},
toolbox: {
feature: {
saveAsImage: {},
},
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true,
},
xAxis: [
{
type: 'category',
boundaryGap: false,
data: ['0', '1', '2', '3', '4', '5', '6'],
},
],
yAxis: [
{
type: 'value',
},
],
series: [
{
name: 'stack 1',
type: 'line',
areaStyle: {
color: 'red',
},
data: [140 + 100, 150 + 110, 160 + 120, 180 + 130, 160 + 90, 240 + 130, 160 + 120],
},
{
name: 'stack 2',
type: 'line',
areaStyle: {
color: 'green',
},
data: [120 + 100, 140 + 110, 130 + 120, 150 + 130, 120 + 90, 160 + 130, 125 + 120],
},
{
name: 'basis',
type: 'line',
areaStyle: {
color: 'blue',
},
data: [100, 110, 120, 130, 90, 130, 120],
},
],
};
but this way I will loose the interactive functionalities of ECharts.
Is there a possible way to do this? To stack the red and the green chart on the blue one but not on each other?
You need to do the same all series' names like this.
series: [
{
name: 'stack',
type: 'line',
areaStyle: {
color: 'red',
},
data: [140, 150, 160, 180, 160, 240, 160],
},
{
name: 'stack',
type: 'line',
areaStyle: {
color: 'green',
},
data: [120, 140, 130, 150, 120, 160, 125],
},
{
name: 'stack',
type: 'line',
areaStyle: {
color: 'blue',
},
data: [100, 110, 120, 130, 90, 130, 120],
},
],
and also, you can modify the tooltip with this
I hope, it will be helpful for you.
I found a simple solution for this by duplicating the basis graph with the very same data and colors, so the user doesn't see two graphs, then stack the red graph on one example of blue, and the green one on the other copy of the blue one like this:
option = {
legend: {
data: ['blue', 'red', 'green'],
},
xAxis: [
{
boundaryGap: false,
data: ['0', '1', '2', '3', '4', '5', '6'],
},
],
yAxis: [
{
type: 'value',
},
],
series: [
{
name: 'blue',
stack: 'stack 1',
type: 'line',
areaStyle: {
color: 'blue',
},
data: [100, 110, 120, 130, 90, 130, 120],
},
{
name: 'blue',
stack: 'stack 2',
type: 'line',
areaStyle: {
color: 'blue',
},
data: [100, 110, 120, 130, 90, 130, 120],
},
{
name: 'red',
stack: 'stack 1',
type: 'line',
areaStyle: {
color: 'red',
},
data: [140, 150, 160, 180, 160, 240, 160],
},
{
name: 'green',
stack: 'stack 2',
type: 'line',
areaStyle: {
color: 'green',
},
data: [120, 140, 130, 150, 120, 160, 125],
},
],
};
This will insure that both copies of the blue graph (standing as basis for both red and green one) will be disabled/enabled together since they have the same name, and each one of the the red and green graphs is stacked on a different blue copy.

Highstocks chart multiple pane width and y axis alignment issue

I have issue with the width and alignment of y axis for each pane in my highstock multiple pane chart.
This issue is observed only if there are more than 2 panes in the highstocks chart.
What is the issue here?
$(function() {
var groupingUnits = [
[
'minute', // unit name
[60] // allowed multiples
],
[
'day', [1]],
[
'week', [1]],
[
'month', [1]]
];
Highcharts.setOptions({
global: {
useUTC: false
}
});
his_WscaleTranstek_chart = Highcharts.stockChart('container', {
plotOptions: {
series: {
dataGrouping: {
units: groupingUnits
},
marker: {
enabled: true,
symbol: 'cirlce',
states: {
select: {
fillColor: 'red',
lineWidth: 0,
radius: 5
}
}
},
cursor: 'pointer',
}
},
rangeSelector: {
selected: 2,
buttons: [{
type: 'minute',
count: 60,
text: '1h'
}, {
type: 'day',
count: 1,
text: '1d'
}, {
type: 'week',
count: 1,
text: '1w'
}, {
type: 'month',
count: 1,
text: '1m'
}, {
type: 'month',
count: 3,
text: '3m'
}, {
type: 'month',
count: 6,
text: '6m'
}, {
type: 'year',
count: 1,
text: '1y'
}, {
type: 'all',
text: 'All'
}]
},
yAxis: [
{
opposite: false,
min: 0,
max: 50,
tickInterval: 5,
top:'0%',
height: '15%',
labels: {
align: 'right',
x: -3
},
title: {
text: 'y Axis 1 '
},
plotBands: [{
from: 0,
to: 16,
color: 'rgba(16, 180, 146, 0.56)'
}, {
from: 16,
to: 19,
color: 'rgba(0, 195, 206, 0.5)'
}, {
from: 19,
to: 25,
color: 'rgba(7, 235, 78, 0.68)'
}, {
from: 25,
to: 30,
color: 'rgba(225, 239, 6, 0.68)'
}, {
from: 30,
to: 40,
color: 'rgba(239, 134, 5, 0.5)'
}, {
from: 40,
to: 50,
color: 'rgba(251, 44, 6, 0.5)'
}
],
lineWidth: 2,
//maxPadding: 0,
endOnTick: false,
gridLineWidth: 0
}, {
opposite: false,
min: 20,
max: 280,
tickInterval: 10,
labels: {
align: 'right',
x: -3
},
title: {
text: 'y Axis 2'
},
lineWidth: 1,
top: '17%',
height: '15%',
offset: 0,
//maxPadding: 0,
endOnTick: false,
gridLineWidth: 0
},{
opposite: false,
min: 0,
max: 50,
tickInterval: 5,
top: '34%',
height: '15%',
labels: {
align: 'right',
x: -3
},
title: {
text: 'y Axis 3'
},
lineWidth: 2,
//maxPadding: 0,
endOnTick: false,
gridLineWidth: 0
},
{
opposite: false,
min: 0,
max: 80,
tickInterval: 5,
top: '51%',
height: '15%',
labels: {
align: 'right',
x: -3
},
title: {
text: 'y Axis 4'
},
plotBands: [],
lineWidth: 2,
maxPadding: 0,
endOnTick: false,
gridLineWidth: 0
},
{
opposite: false,
min: 10,
max: 70,
tickInterval: 5,
top: '68%',
height: '15%',
labels: {
align: 'right',
x: -3
},
title: {
text: 'y Axis 5'
},
plotBands: [],
lineWidth: 2,
maxPadding: 0,
endOnTick: false,
gridLineWidth: 0
},{
opposite: false,
min: 0,
max: 8,
tickInterval: 5,
top: '85%',
height: '15%',
labels: {
align: 'right',
x: -3
},
title: {
text: 'y Axis 6'
},
plotBands: [],
lineWidth: 2,
maxPadding: 0,
endOnTick: false,
gridLineWidth: 0
}
],
title: {
text: 'Chart',
style: {
color: '#4e80b2'
}
},
legend: {
enabled: false,
itemStyle: {
'cursor': 'default'
}
},
series: [
{
type: 'line',
name: 'BMI',
data: [15,15,15,15,15,15,15],
lineColor: "#777d7d",
"tooltip": {
pointFormatter: function () {
var point = this;
return '<span style="color:' + point.color + '">\u25CF</span> ' + point.series.name + ': <b>' + Math.round((point.y)) + ' </b><br/>';
}
},
marker: {
fillColor: 'white',
lineWidth: 2,
lineColor: "#777d7d"
},
yAxis: 0,
showInLegend: false
},
{
type: 'line',
name: 'WEIGHT',
data: [15,15,15,15,15,15,15],
lineColor: "#777d7d",
"tooltip": {
pointFormatter: function () {
var point = this;
return '<span style="color:' + point.color + '">\u25CF</span> ' + point.series.name + ': <b>' + (point.y) + ' kg</b><br/>';
}
},
marker: {
fillColor: 'white',
lineWidth: 2,
lineColor: "#777d7d"
},
yAxis: 1,
showInLegend: false
},
{
type: 'line',
name: 'Fat %',
data: [15,15,15,15,15,15,15],
lineColor: "#777d7d",
"tooltip": {
pointFormatter: function () {
var point = this;
return '<span style="color:' + point.color + '">\u25CF</span> ' + point.series.name + ': <b>' + (point.y) + ' kg</b><br/>';
}
},
marker: {
fillColor: 'white',
lineWidth: 2,
lineColor: "#777d7d"
},
yAxis: 2,
showInLegend: false
},
{
type: 'line',
name: 'Water %',
data: [15,15,15,15,15,15,15],
lineColor: "#777d7d",
"tooltip": {
pointFormatter: function () {
var point = this;
return '<span style="color:' + point.color + '">\u25CF</span> ' + point.series.name + ': <b>' + (point.y) + ' kg</b><br/>';
}
},
marker: {
fillColor: 'white',
lineWidth: 2,
lineColor: "#777d7d"
},
yAxis: 3,
showInLegend: false
},
{
type: 'line',
name: 'Muscle mass',
data: [15,15,15,15,15,15,15],
lineColor: "#777d7d",
"tooltip": {
pointFormatter: function () {
var point = this;
return '<span style="color:' + point.color + '">\u25CF</span> ' + point.series.name + ': <b>' + (point.y) + ' kg</b><br/>';
}
},
marker: {
fillColor: 'white',
lineWidth: 2,
lineColor: "#777d7d"
},
yAxis: 4,
showInLegend: false
},
{
type: 'line',
name: 'Bone Weight',
data: [15,15,15,15,15,15,15],
lineColor: "#777d7d",
"tooltip": {
pointFormatter: function () {
var point = this;
return '<span style="color:' + point.color + '">\u25CF</span> ' + point.series.name + ': <b>' + (point.y) + ' kg</b><br/>';
}
},
marker: {
fillColor: 'white',
lineWidth: 2,
lineColor: "#777d7d"
},
yAxis: 5,
showInLegend: false
}
],
navigator: {
height: 30
}
}
)
});
jsfiddle
You should set yAxis.offset for the axes.
yAxis: [{
offset: 0,
example: https://jsfiddle.net/gahjh1uz/

Html file not reading array from javascript file as input

This is my app.js file
var earn=[' ',' ',' ',' ',' '];
var lost=['50','60','100','40','20'];
var Break=['40','30','90','90','40'];
var win = Titanium.UI.createWindow({
backgroundColor: 'white'
});
var lbl1 = Ti.UI.createLabel({
backgroundColor: '#CED8F6',
height:'5%',
width:'10%',
top:0,
left:0,
borderWidth: 2,
borderColor: 'black',
});
var lbl2 = Ti.UI.createLabel({
backgroundColor: 'white',
text:'Earn',
height:'5%',
width:'10%',
top:'5%',
left:0,
color: 'black',
borderWidth: 2,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl2);
var lbl3 = Ti.UI.createLabel({
backgroundColor: '#CED8F6',
text:'Lost',
height:'5%',
width:'10%',
top:'10%',
left:0,
color: 'black',
borderWidth: 2,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl3);
var lbl4 = Ti.UI.createLabel({
backgroundColor: 'white',
text:'Break',
height:'5%',
width:'10%',
top:'15%',
left:0,
color: 'black',
borderWidth: 2,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl4);
var lbl5 = Ti.UI.createLabel({
backgroundColor: 'white',
text:'Jan',
height:'5%',
width:'10%',
top:0,
left:'10%',
color: 'black',
borderWidth: 2,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl5);
var textfield1 = Ti.UI.createTextField({
backgroundColor: '#CED8F6',
value:earn[0],
height:'5%',
width:'10%',
top:'5%',
left:'10%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
keyboardType: Titanium.UI.KEYBOARD_NUMBER_PAD,
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(textfield1);
var lbl7 = Ti.UI.createLabel({
backgroundColor: 'white',
text:lost[0],
height:'5%',
width:'10%',
top:'10%',
left:'10%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl7);
var lbl8 = Ti.UI.createLabel({
backgroundColor: '#CED8F6',
text:Break[0],
height:'5%',
width:'10%',
top:'15%',
left:'10%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl8);
var lbl9 = Ti.UI.createLabel({
text:'Feb',
backgroundColor: '#CED8F6',
height:'5%',
width:'10%',
left:'20%',
top:'0%',
color:'black',
borderWidth: 2,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl9);
var textfield2 = Ti.UI.createTextField({
backgroundColor: 'white',
value:earn[1],
height:'5%',
width:'10%',
top:'5%',
left:'20%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
keyboardType: Titanium.UI.KEYBOARD_NUMBER_PAD,
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(textfield2);
var lbl11 = Ti.UI.createLabel({
backgroundColor: '#CED8F6',
text:lost[1],
height:'5%',
width:'10%',
top:'10%',
left:'20%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl11);
var lbl12 = Ti.UI.createLabel({
backgroundColor: 'white',
text:Break[1],
height:'5%',
width:'10%',
top:'15%',
left:'20%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl12);
var lbl13 = Ti.UI.createLabel({
backgroundColor: 'white',
text:'March',
height:'5%',
width:'10%',
top:0,
left:'30%',
color: 'black',
borderWidth: 2,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl13);
var textfield3 = Ti.UI.createTextField({
backgroundColor: '#CED8F6',
value:earn[2],
height:'5%',
width:'10%',
top:'5%',
left:'30%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
keyboardType: Titanium.UI.KEYBOARD_NUMBER_PAD,
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(textfield3);
var lbl15 = Ti.UI.createLabel({
backgroundColor: 'white',
text:lost[2],
height:'5%',
width:'10%',
top:'10%',
left:'30%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl15);
var lbl16 = Ti.UI.createLabel({
backgroundColor: '#CED8F6',
text:Break[2],
height:'5%',
width:'10%',
top:'15%',
left:'30%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl16);
var lbl17 = Ti.UI.createLabel({
text:'Apr',
backgroundColor: '#CED8F6',
height:'5%',
width:'10%',
left:'40%',
top:'0%',
color:'black',
borderWidth: 2,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl17);
var textfield4 = Ti.UI.createTextField({
backgroundColor: 'white',
value:earn[3],
height:'5%',
width:'10%',
top:'5%',
left:'40%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
keyboardType: Titanium.UI.KEYBOARD_NUMBER_PAD,
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(textfield4);
var lbl19 = Ti.UI.createLabel({
backgroundColor: '#CED8F6',
text:lost[3],
height:'5%',
width:'10%',
top:'10%',
left:'40%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl19);
var lbl20 = Ti.UI.createLabel({
backgroundColor: 'white',
text:Break[3],
height:'5%',
width:'10%',
top:'15%',
left:'40%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl20);
var lbl21= Ti.UI.createLabel({
backgroundColor: 'white',
text:'May',
height:'5%',
width:'10%',
top:0,
left:'50%',
color: 'black',
borderWidth: 2,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl21);
var textfield5 = Ti.UI.createTextField({
backgroundColor: '#CED8F6',
value:earn[4],
height:'5%',
width:'10%',
top:'5%',
left:'50%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
keyboardType: Titanium.UI.KEYBOARD_NUMBER_PAD,
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(textfield5);
var lbl23 = Ti.UI.createLabel({
backgroundColor: 'white',
text:lost[4],
height:'5%',
width:'10%',
top:'10%',
left:'50%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl23);
var lbl24 = Ti.UI.createLabel({
backgroundColor: '#CED8F6',
text:Break[4],
height:'5%',
width:'10%',
top:'15%',
left:'50%',
color: 'black',
borderWidth: 1,
borderColor: 'black',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
});
win.add(lbl24);
var btn = Ti.UI.createButton({
title: 'Bar Chart',
bottom: '5%',
left: '2%',
width: '10%'
});
btn.addEventListener('click', function(e)
{
win.remove(layer);
win.add(webview);
});
//win.add(webview);
var btn1 = Ti.UI.createButton({
title: 'Pie Chart',
bottom: '12%',
left: '2%',
width: '10%'
});
btn1.addEventListener('click', function(e)
{
earn[0] = textfield1.value;
earn[1] = textfield2.value;
earn[2] = textfield3.value;
earn[3] = textfield4.value;
earn[4] = textfield5.value;
alert(earn[0]);
win.add(layer);
win.remove(webview);
});
var webview = Titanium.UI.createWebView({
url:'chart.html',
top: '25%',
left: '12%',
});
webview.addEventListener('beforeload',function(e)
{
earn[0] = textfield1.value;
earn[1] = textfield2.value;
earn[2] = textfield3.value;
earn[3] = textfield4.value;
earn[4] = textfield5.value;
wbv1.evalJS("var earn[0] ='"+earn[0]+"';");
wbv1.evalJS("var earn[1] ='"+earn[1]+"';");
wbv1.evalJS("var earn[2] ='"+earn[2]+"';");
wbv1.evalJS("var earn[3] ='"+earn[3]+"';");
wbv1.evalJS("var earn[4] ='"+earn[4]+"';");
});
var layer = Ti.UI.createView({
top: '25%',
left: '12%',
bottom: '0%',
});
//win.add(layer);
var wbv1 = Ti.UI.createWebView({
url:'pie1.html',
top: '0%',
left: '0%',
right:'67%',
//bottom: 0,
});
wbv1.addEventListener('afterload',function(e)
{
earn[0] = textfield1.value;
earn[1] = textfield2.value;
earn[2] = textfield3.value;
earn[3] = textfield4.value;
earn[4] = textfield5.value;
wbv1.evalJS("var earn[0] ='"+earn[0]+"';");
wbv1.evalJS("var earn[1] ='"+earn[1]+"';");
wbv1.evalJS("var earn[2] ='"+earn[2]+"';");
wbv1.evalJS("var earn[3] ='"+earn[3]+"';");
wbv1.evalJS("var earn[4] ='"+earn[4]+"';");
});
layer.add(wbv1);
var wbv2 = Ti.UI.createWebView({
url:'pie2.html',
top: '0%',
left: '33%',
right:'33%',
//bottom: 0,
});
layer.add(wbv2);
var wbv3 = Ti.UI.createWebView({
url:'pie3.html',
top: '0%',
left: '67%',
right:'0%',
//bottom: 0,
});
layer.add(wbv3);
win.add(btn1);
win.add(btn);
win.add(lbl1);
win.open();
and this is my pie1.html
<html>
<head>
<title> Earn </title>
<script language="javascript" src="http://www.google.com/jsapi"></script>
<script language="javascript" src="app.js" type="text/javascript"></script>
<meta name="viewport" content="user-scalable=0">
</head>
<body>
<div id="chart"></div>
<script type="text/javascript">
document.ontouchmove = function(event){
event.preventDefault();
}
var queryString = '';
var dataUrl = '';
function onLoadCallback() {
if (dataUrl.length > 0) {
var query = new google.visualization.Query(dataUrl);
query.setQuery(queryString);
query.send(handleQueryResponse);
} else {
var dataTable = new google.visualization.DataTable();
dataTable.addRows(5);
dataTable.addColumn('number');
dataTable.setValue(0, 0, +earn[0]);
dataTable.setValue(1, 0, +earn[1]);
dataTable.setValue(2, 0, +earn[2]);
dataTable.setValue(3, 0, +earn[3]);
dataTable.setValue(4, 0, +earn[4]);
draw(dataTable);
}
}
function draw(dataTable) {
var vis = new google.visualization.ImageChart(document.getElementById('chart'));
var options = {
chxs: '0,000000,11.5', //size of the pie
chxt: 'x',
chs: '360x265', //size of pie
cht: 'p',
chco: 'FF9900', //color of the pie
chd: 's:9flxY',
chdl: earn[0]+'|'+earn[1]+'|'+earn[2]+'|'+earn[3]+'|'+earn[4],
chl: 'Jan|Feb|March|Apr|May',
chma: '25',
chtt: 'Earn',
chts: '000000,14.5',
bottom: '0', //word size of the pie
};
vis.draw(dataTable, options);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
draw(response.getDataTable());
}
/* document.body.addEventListener('touchmove', function(e) {
// This prevents native scrolling from happening.
we e.preventDefault();
}, false);*/
google.load("visualization", "1", {packages:["imagechart"]});
google.setOnLoadCallback(onLoadCallback);
</script>
</body>
</html>
Im trying for my 1st pie as input data on the table and pass the array to html file so that the pie can detect the array and plot itself, but now the problem is that it is not showing the correct data i input. it will just trace the global variable as (' ') nothing instead of the one i input. Sorry for the long coding, im still new, that's the only way i know how to create a table.
I did not go through all your code (there is too much) but I did notice that you do this:
webview.addEventListener('beforeload',function(e)
{
earn[0] = textfield1.value;
earn[1] = textfield2.value;
earn[2] = textfield3.value;
earn[3] = textfield4.value;
earn[4] = textfield5.value;
wbv1.evalJS("var earn[0] ='"+earn[0]+"';");
wbv1.evalJS("var earn[1] ='"+earn[1]+"';");
wbv1.evalJS("var earn[2] ='"+earn[2]+"';");
wbv1.evalJS("var earn[3] ='"+earn[3]+"';");
wbv1.evalJS("var earn[4] ='"+earn[4]+"';");
});
You can't call evalJS on a webview till after the load event has been fired, evalJS only works if there is a page with a DOM that has been loaded first, so this wont do anything. You need to only call evalJS on a load event.