Rotate chart title in Echarts? - echarts

In Apache Echarts, is there a way to rotate the title text of a chart so that it is vertically oriented on one side rather than horizontal on the top? The title at the top sometimes takes important chart real-estate that my series in some cases can overlap. I would like to instruct Echarts when there's a space crunch to move the title off to the left side in a vertical position (like reading titles on the spines of books in a library). There appears to be several "rotate" options for labels, but nothing for titles.

I am not sure you can rotate the main title if you use the default canvas renderer. In this case, you may want to play with the name of your yAxis like this:
const myChart = echarts.init(document.getElementById('main'));
let option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value',
name: 'This is my title',
nameLocation: 'center',
nameRotate: 90,
nameGap: 45,
nameTextStyle: {
color: 'black',
fontSize: 18,
fontWeight: 'bold'
}
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line'
}
]
};
myChart.setOption(option);
#main {
width: 100%;
height: 350px;
}
<script src="https://cdn.jsdelivr.net/npm/echarts#5.4.1/dist/echarts.min.js"></script>
<div id="main"></div>
Alternatively, you could use the SVG renderer and add a bit of CSS to transform the main title (which is a <text> element):
const myChart = echarts.init(document.getElementById('main'), null, { renderer: 'svg' });
let option = {
title: {
text: 'This is my title'
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line'
}
]
};
myChart.setOption(option);
#main {
width: 100%;
height: 350px;
}
svg g text:last-child {
transform: rotate(-90deg) translateX(-250px);
}
<script src="https://cdn.jsdelivr.net/npm/echarts#5.4.1/dist/echarts.min.js"></script>
<div id="main"></div>

Related

Apache Echarts - change the color of grid elements

Hi,
How can I change the colors of the lines and text in the image bellow.
https://codesandbox.io/s/wy90g2?file=/index.js
option = {
grid:{
show: false
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar'
}
]
};
Thanks

Echarts - Rotated X-Axis labels overlap the graph itself

I am using Apache Echarts 5.2.2 and I am struggling with rotated x-axis labels. With large font sizes like 30, 40, they overlap the graph as you can see in the image below
Here the config
option = {
grid: {
left: '10%',
right: '10%',
bottom: '15%'
},
xAxis: {
type: 'category',
data: [
"Arta",
"Chahar Mahal va Bakhtiari",
"Drenthe",
"Gamprin",
"Kabale",
"Lvivska oblast",
"Tebessa",
"Wangdue Phodrang",
"Zagorje ob Savi",
"Zeeland"
],
axisLabel: {
fontSize: 40,
rotate: -45,
overflow: 'truncate',
width: 100,
}
},
yAxis: {
type: 'value',
name: 'km/s minus 299,000',
},
};
Anyone knows the solution or what I am doing wrong?

eCharts: How to change line colors for positive and negative values?

I want to color the area "under" the graph (i.e. between zero and value) green when positive, and red when negative, on an eCharts line graph.
Like this
We have already done it with a bar graph (below), but now we need to do it with a line graph.
You can achieve this by setting the visualMap property. I have done some hit and trial and achieved the following.
var myChart = echarts.init(document.getElementById('main'));
option = {
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [820, 932, 901, -1200, -800, 1330, 1320],
type: 'line',
areaStyle: {}
}],
visualMap: {
left: 'right',
min: 0,
max: 1,
inRange: {
color: ['red', 'green']
},
text: ['>0', '<0'],
calculable: true
},
};
// use configuration item and data specified to show chart
myChart.setOption(option);
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.6.0/echarts.min.js"></script>
<div id="main" style="width: 600px;height:400px;"></div>

Highchart multi Yaxis in one line

I want to create chart like that in highcharts:
IOT Central Chart
this is what i get until now:
Highcharts.setOptions({
colors: ['#3399CF', '#F9BA06', '#65AF35', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4']
});
// Get the data. The contents of the data file can be viewed at
$.getJSON(
'https://cdn.rawgit.com/highcharts/highcharts/057b672172ccc6c08fe7dbb27fc17ebca3f5b770/samples/data/activity.json',
function (activity) {
$.each(activity.datasets, function (i, dataset) {
// Add X values
dataset.data = Highcharts.map(dataset.data.splice(1, 10), function (val, j) {
return [activity.xData[j], val];
});
$('<div class="chart">')
.appendTo('#container')
.highcharts({
chart: {
type: "spline",
marginLeft: 40, // Keep all charts left aligned
marginTop: 7,
marginBottom: 7
},
title: {
text: null,
},
credits: {
enabled: false
},
legend: {
enabled: false
},
xAxis: {
visible: false,
labels: {
format: '{value} km'
}
},
yAxis: {
visible: true,
title: {
text: null
},
tickAmount: 2,
minPadding: 0,
lineWidth:1,
gridLineColor: "transparent"
},
series: [{
data: dataset.data,
name: dataset.name,
color: Highcharts.getOptions().colors[i],
fillOpacity: 0.3,
tooltip: {
valueSuffix: ' ' + dataset.unit
}
}]
});
});
}
);
.chart {
min-width: 320px;
max-width: 800px;
height: 150px;
margin: 0 auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
but the problem is that i cant export or zoom do normal legend to this chart.
The problem here is that you create three separate charts instead of one.
Highstock allows you to position and resize axes (height & top properties):
yAxis: [{
height: '50%'
}, {
top: '50%',
height: '50%'
}],
API references:
https://api.highcharts.com/highstock/yAxis.height
https://api.highcharts.com/highstock/yAxis.top
In fact it's going to work in Highcharts too (even though it's not documented).
Live demo: http://jsfiddle.net/BlackLabel/gb3jhq75/

Google charts - ComboChart - bars and candlesticks

I try create ComboChart where are bars and series of waterfall(candlestick) chart.
I dont know how to create data input for this.
Candlestick needs format like:
['Mon', 20, 28, 38, 45],
['Tue', 31, 38, 55, 66],
['Wed', 50, 55, 77, 80],
['Thu', 77, 77, 66, 50],
['Fri', 68, 66, 22, 15]
and bars in my case:
['name', 'value', { role: 'style' }],
['test1', 8, '#00f'],
['test2', 10,'#000'],
['test3', 19,'#f00'],
Is it even possible to do it ? Join bars and candlesticks ?
use the series option to control the chart type for each series
each series is represented by a column, or a set of columns, in the data
starting with the first column that is not the x-axis or a column role
example: the columns for the chart you describe might resemble the following...
"cols":[
// x-axis
{"label":"Category","type":"string"},
// bar series -- 0
{"label":"Bar 0","type":"number"},
// bar series -- 1
{"label":"Bar 1","type":"number"},
// waterfall series -- 2
{"label":"WF 2 - Bottom 1","type":"number"},
{"label":"WF 2 - Bottom 2","type":"number"},
{"label":"WF 2 - Top 1","type":"number"},
{"label":"WF 2 - Top 2","type":"number"},
{"role":"style","type":"string","p":{"role":"style"}},
// bar series -- 3
{"label":"Bar 3","type":"number"}
],
the candlestick chart requires 4 columns, in the above example, a style column role is also used,
as such, all five columns are considered part of series number 2
when building data for a combochart,
use null for the series values,
when they do not coincide with the same x-axis value
example data:
"rows":[
// bar (series 0 & 1) data
{"c":[{"v":"YTD"},{"v":14807893.054},{"v":11684139.912},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null}]},
{"c":[{"v":"Last Year"},{"v":16307893.054},{"v":20684139.912},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null}]},
// waterfall (series 2) data
{"c":[{"v":"Budget"},{"v":null},{"v":null},{"v":0},{"v":0},{"v":22707893.613},{"v":22707893.613},{"v":"#007fff"},{"v":null}]},
{"c":[{"v":"Contract Labor"},{"v":null},{"v":null},{"v":22707893.613},{"v":22707893.613},{"v":22534350.429},{"v":22534350.429},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Contract Non Labor"},{"v":null},{"v":null},{"v":22534350.429},{"v":22534350.429},{"v":22930956.493},{"v":22930956.493},{"v":"#922b21"},{"v":null}]},
{"c":[{"v":"Matls & Equip"},{"v":null},{"v":null},{"v":22930956.493},{"v":22930956.493},{"v":22800059.612},{"v":22800059.612},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Other"},{"v":null},{"v":null},{"v":22800059.612},{"v":22800059.612},{"v":21993391.103},{"v":21993391.103},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Labor"},{"v":null},{"v":null},{"v":21993391.103},{"v":21993391.103},{"v":21546003.177999996},{"v":21546003.177999996},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Travel"},{"v":null},{"v":null},{"v":21546003.177999996},{"v":21546003.177999996},{"v":21533258.930999994},{"v":21533258.930999994},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Training"},{"v":null},{"v":null},{"v":21533258.930999994},{"v":21533258.930999994},{"v":21550964.529999994},{"v":21550964.529999994},{"v":"#922b21"},{"v":null}]},
{"c":[{"v":"Actual"},{"v":null},{"v":null},{"v":0},{"v":0},{"v":21550964.52999999},{"v":21550964.52999999},{"v":"#007fff"},{"v":null}]},
// bar (series 3) data
{"c":[{"v":"FCST"},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":16182561.25}]}
]
following is a working example of a combo chart with bars and candlesticks
google.charts.load('current', {
callback: function () {
drawChart();
$(window).resize(drawChart);
},
packages:['corechart', 'table']
});
function drawChart() {
var dataChart = new google.visualization.DataTable({
"cols":[
// x-axis
{"label":"Category","type":"string"},
// bar series -- 0
{"label":"Bar 0","type":"number"},
// bar series -- 1
{"label":"Bar 1","type":"number"},
// waterfall series -- 2
{"label":"WF 2 - Bottom 1","type":"number"},
{"label":"WF 2 - Bottom 2","type":"number"},
{"label":"WF 2 - Top 1","type":"number"},
{"label":"WF 2 - Top 2","type":"number"},
{"role":"style","type":"string","p":{"role":"style"}},
// bar series -- 3
{"label":"Bar 3","type":"number"}
],
"rows":[
// bar (series 0 & 1) data
{"c":[{"v":"YTD"},{"v":14807893.054},{"v":11684139.912},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null}]},
{"c":[{"v":"Last Year"},{"v":16307893.054},{"v":20684139.912},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null}]},
// waterfall (series 2) data
{"c":[{"v":"Budget"},{"v":null},{"v":null},{"v":0},{"v":0},{"v":22707893.613},{"v":22707893.613},{"v":"#007fff"},{"v":null}]},
{"c":[{"v":"Contract Labor"},{"v":null},{"v":null},{"v":22707893.613},{"v":22707893.613},{"v":22534350.429},{"v":22534350.429},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Contract Non Labor"},{"v":null},{"v":null},{"v":22534350.429},{"v":22534350.429},{"v":22930956.493},{"v":22930956.493},{"v":"#922b21"},{"v":null}]},
{"c":[{"v":"Matls & Equip"},{"v":null},{"v":null},{"v":22930956.493},{"v":22930956.493},{"v":22800059.612},{"v":22800059.612},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Other"},{"v":null},{"v":null},{"v":22800059.612},{"v":22800059.612},{"v":21993391.103},{"v":21993391.103},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Labor"},{"v":null},{"v":null},{"v":21993391.103},{"v":21993391.103},{"v":21546003.177999996},{"v":21546003.177999996},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Travel"},{"v":null},{"v":null},{"v":21546003.177999996},{"v":21546003.177999996},{"v":21533258.930999994},{"v":21533258.930999994},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Training"},{"v":null},{"v":null},{"v":21533258.930999994},{"v":21533258.930999994},{"v":21550964.529999994},{"v":21550964.529999994},{"v":"#922b21"},{"v":null}]},
{"c":[{"v":"Actual"},{"v":null},{"v":null},{"v":0},{"v":0},{"v":21550964.52999999},{"v":21550964.52999999},{"v":"#007fff"},{"v":null}]},
// bar (series 3) data
{"c":[{"v":"FCST"},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":16182561.25}]}
]
});
var comboChart = new google.visualization.ChartWrapper({
chartType: 'ComboChart',
containerId: 'chart_div',
dataTable: dataChart,
options: {
animation: {
duration: 1500,
easing: 'inAndOut',
startup: true
},
backgroundColor: 'transparent',
bar: {
group: {
width: '85%'
}
},
chartArea: {
backgroundColor: 'transparent',
bottom: 42,
height: '100%',
left: 60,
top: 24,
width: '100%'
},
hAxis: {
textStyle: {
fontSize: 12
}
},
height: 400,
legend: 'none',
seriesType: 'bars',
series: {
2: {
type: 'candlesticks'
}
},
tooltip: {
isHtml: true,
trigger: 'both'
},
vAxis: {
format: 'short',
textStyle: {
color: '#616161'
},
viewWindow: {
min: 10000000,
max: 24000000
}
},
width: '100%'
}
});
comboChart.draw();
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div id="chart_div"></div>
note: you'll notice when running the snippet in full page mode,
the chart doesn't do a very good job of aligning the x-axis labels
you may be able to manipulate the options to get them to align better,
but it will take some manipulation...
you might have better results, creating three separate charts, side-by-side
just need to make sure each has the same vAxis values,
you could hide the extra vAxis labels if needed, etc...
again, manipulation is required
example: snippet displayed best in full page mode...
google.charts.load('current', {
callback: function () {
drawChart();
$(window).resize(drawChart);
},
packages:['corechart', 'table']
});
function drawChart() {
var dataBar0 = new google.visualization.DataTable({
"cols":[
{"label":"Category","type":"string"},
{"label":"Bar 0","type":"number"},
{"label":"Bar 1","type":"number"}
],
"rows":[
// bar 0 data
{"c":[{"v":"YTD"},{"v":22807893.054},{"v":18684139.912}]}
]
});
var barChart0 = new google.visualization.ChartWrapper({
chartType: 'ColumnChart',
containerId: 'bar_chart_0',
dataTable: dataBar0,
options: {
animation: {
duration: 1500,
easing: 'inAndOut',
startup: true
},
backgroundColor: 'transparent',
bar: {
gap: 20,
width: 60
},
chartArea: {
backgroundColor: 'transparent',
},
height: 400,
legend: 'none',
theme: 'maximized',
tooltip: {
isHtml: true,
trigger: 'both'
},
vAxis: {
format: 'short',
viewWindow: {
min: 10000000,
max: 24000000
}
}
}
});
barChart0.draw();
var dataWF0 = new google.visualization.DataTable({
"cols":[
{"label":"Category","type":"string"},
{"label":"WF 2 - Bottom 1","type":"number"},
{"label":"WF 2 - Bottom 2","type":"number"},
{"label":"WF 2 - Top 1","type":"number"},
{"label":"WF 2 - Top 2","type":"number"},
{"role":"style","type":"string","p":{"role":"style"}}
],
"rows":[
{"c":[{"v":"Budget"},{"v":0},{"v":0},{"v":22707893.613},{"v":22707893.613},{"v":"#007fff"}]},
{"c":[{"v":"Other"},{"v":22800059.612},{"v":22800059.612},{"v":21993391.103},{"v":21993391.103},{"v":"#1e8449"}]},
{"c":[{"v":"Labor"},{"v":21993391.103},{"v":21993391.103},{"v":21546003.177999996},{"v":21546003.177999996},{"v":"#1e8449"}]},
{"c":[{"v":"Travel"},{"v":21546003.177999996},{"v":21546003.177999996},{"v":21533258.930999994},{"v":21533258.930999994},{"v":"#1e8449"}]},
{"c":[{"v":"Training"},{"v":21533258.930999994},{"v":21533258.930999994},{"v":21550964.529999994},{"v":21550964.529999994},{"v":"#922b21"}]},
{"c":[{"v":"Actual"},{"v":0},{"v":0},{"v":21550964.52999999},{"v":21550964.52999999},{"v":"#007fff"}]}
]
});
var wfChart0 = new google.visualization.ChartWrapper({
chartType: 'CandlestickChart',
containerId: 'wf_chart_0',
dataTable: dataWF0,
options: {
animation: {
duration: 1500,
easing: 'inAndOut',
startup: true
},
backgroundColor: 'transparent',
bar: {
gap: 20,
width: 60
},
chartArea: {
backgroundColor: 'transparent'
},
height: 400,
legend: 'none',
theme: 'maximized',
tooltip: {
isHtml: true,
trigger: 'both'
},
vAxis: {
format: 'short',
viewWindow: {
min: 10000000,
max: 24000000
}
},
width: '100%'
}
});
wfChart0.draw();
var dataBar1 = new google.visualization.DataTable({
"cols":[
{"label":"Category","type":"string"},
{"label":"Bar 0","type":"number"},
{"label":"Bar 1","type":"number"}
],
"rows":[
// bar 0 data
{"c":[{"v":"YTD"},{"v":20807893.054},{"v":19684139.912}]}
]
});
var barChart1 = new google.visualization.ChartWrapper({
chartType: 'ColumnChart',
containerId: 'bar_chart_1',
dataTable: dataBar1,
options: {
animation: {
duration: 1500,
easing: 'inAndOut',
startup: true
},
backgroundColor: 'transparent',
bar: {
gap: 20,
width: 60
},
chartArea: {
backgroundColor: 'transparent',
},
height: 400,
legend: 'none',
theme: 'maximized',
tooltip: {
isHtml: true,
trigger: 'both'
},
vAxis: {
format: 'short',
viewWindow: {
min: 10000000,
max: 24000000
}
}
}
});
barChart1.draw();
}
.max-chart {
display: inline-block;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="max-chart" id="bar_chart_0"></div>
<div class="max-chart" id="wf_chart_0"></div>
<div class="max-chart" id="bar_chart_1"></div>