Is it possible in ZingChart to add a secondary y scale that uses the same values as the primary y scale, but just uses a simple conversion (e.g., anomaly degrees Celsius*1.8 = anomaly degrees Fahrenheit).
something like:
var chartConfig = {
scaleY2: { format: %v*1.8 }
}
Or, perhaps a function, like:
var chartConfig = {
scaleY2: { format: 'formatAxis()' }
}
...
formatAxis = function(p){ return { format:p.value*1.8 } }
I'm plotting temperature anomalies in degrees C on the primary y-axis. I'd like the degrees F to show up on the secondary y-axis.
You do indeed use a function. I just had a syntax error.
var chartConfig = {
scaleY2: { format: 'formatAxis()' }
}
...
window.formatAxis = function(v){
return (v*1.8).toFixed(2)+'\u00B0F';
}
The above answer from #magnum-π is correct. Creating a formatting function is the easiest and most effective solution.
// how to call function from ZingChart
let chartConfig = {
scaleY2: { format: 'formatAxis()' }
}
// defining function for ZingChart to find at the window scope
window.formatAxis = function(v){
return (v*1.8).toFixed(2)+'\u00B0F';
}
I have also configured a working demo of this to assist the above answer:
// window.onload event for Javascript to run after HTML
// because this Javascript is injected into the document head
window.addEventListener('load', () => {
// Javascript code to execute after DOM content
// full ZingChart schema can be found here:
// https://www.zingchart.com/docs/api/json-configuration/
let chartConfig = {
type: 'bar',
globals: {
fontSize: '14px',
},
title: {
text: 'Multiple Scales °C vs °F',
fontSize: '24px',
adjustLayout: true,
},
legend: {
draggable: true,
},
// plot represents general series, or plots, styling
plot: {
// hoverstate
tooltip: {
// % symbol represents a token to insert a value. Full list here:
// https://www.zingchart.com/docs/tutorials/chart-elements/zingchart-tokens/
text: '%kl was %v° %plot-text',
borderRadius: '3px',
// htmlMode renders text attribute as html so
// ° is rendered
htmlMode: true
},
valueBox: {
color: '#fff',
placement: 'top-in'
},
// animation docs here:
// https://www.zingchart.com/docs/tutorials/design-and-styling/chart-animation/#animation__effect
animation: {
effect: 'ANIMATION_EXPAND_BOTTOM',
method: 'ANIMATION_STRONG_EASE_OUT',
sequence: 'ANIMATION_BY_NODE',
speed: 275
}
},
plotarea: { margin: 'dynamic',},
scaleX: {
// set scale label
label: {
text: 'Days'
},
// convert text on scale indices
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
scaleY: {
// scale label with unicode character
label: {
text: 'Temperature (°C)'
}
},
scaleY2: {
label: {
text: 'Temperature (°F)'
},
guide: { visible: false }
},
// plot values
series: [
{
text: 'Celcius',
values: [23, 20, 27, 29, 25, 17, 15],
backgroundColor: '#448aff #64b5f6' ,
scales: 'scale-x, scale-y'
},
{
text: 'Farenheit',
values: [35, 42, 33, 49, 35, 47, 35].map(v => Number((v*1.8).toFixed(2))),
backgroundColor: '#ff5252 #e57373',
scales: 'scale-x, scale-y-2'
}
]
};
// render chart
zingchart.render({
id: 'myChart',
data: chartConfig,
height: '100%',
width: '100%',
});
});
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
.chart--container {
min-height: 150px;
width: 100%;
height: 100%;
}
.zc-ref {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ZingSoft Demo</title>
<script src="https://cdn.zingchart.com/zingchart.min.js"></script>
</head>
<body>
<!-- CHART CONTAINER -->
<div id="myChart" class="chart--container">
<a class="zc-ref" href="https://www.zingchart.com/">Powered by ZingChart</a>
</div>
</body>
</html>
No legends appears in my bar and pie charts.
I tried several arguments for setLegendPlacement and setLegendPosition methods but it was without results.
Where I went wrong ?
This is my config for the bar chart :
BarChartModel model = new BarChartModel();
model.setZoom(true);
model.setLegendPlacement(LegendPlacement.INSIDE);
model.setLegendPosition("ne");
model.setAnimate(true);
model.setMouseoverHighlight(true);
model.setShowPointLabels(true);
ChartSeries myChartSeries = new ChartSeries();
bank.setLabel("myChatSeries");
for (String key : datas.keys()) {
bank.set(key, datas.get(key));
}
model.addSeries(myChartSeries);
Axis yAxis = model.getAxis(AxisType.Y);
yAxis.setLabel(status.name());
yAxis.setMin(0);
yAxis.setMax(maxCount+1);
yAxis.setTickInterval("2");
Axis xAxis = model.getAxis(AxisType.X);
xAxis.setTickAngle(-20);
This is my config for the pie chart :
PieChartModel model = new PieChartModel();
model.setLegendPosition("ne");
model.setLegendPlacement(LegendPlacement.INSIDE);
model.setTitle("Informations général");
model.setMouseoverHighlight(true);
model.setShowDataLabels(true);
model.set("type1", 3);
model.set("type2", 6);
model.set("type3", 8);
My stack : java 8, jsf 2.1.28, primefaces 5.1, primefaces-themes 1.0.10
Resolved by setting the property legend.show to true on a hook before drawing :
$.jqplot.preDrawHooks.push(function () {
this.legend.show = true;
});
A little trashy hack somehow, Primefaces 5.1 doesn't seem to set this property by default.
This question already has an answer here:
Primefaces 5 Rotated Axis Tick Labels
(1 answer)
Closed 7 years ago.
You can see the problem from this site.
Here is the code of my index.html file:
<h:head></h:head>
<h:form>
<p:outputLabel value="Primefaces Tutorial"></p:outputLabel>
<p:chart type="bar" model="#{viewEmployeesManagedBean.barModel}" style="height:500px" xaxisAngle="90" />
</h:form>
</html>
I create a chart in primefaces java. xAxis overlaps. I use xaxisAngle. But it seems it makes no difference. How do I fix it?
I want vertical xAxis labels. What should I do?
The duplicate refers to it doing in the model. You can however also do it in javascript but you need to use the extender functionality. An example of how to use this in this case is
JavaScript:
function customExtender() {
this.cfg.axes = {
xaxis : {
renderer : $.jqplot.DateAxisRenderer,
rendererOptions : {
tickRenderer : $.jqplot.CanvasAxisTickRenderer
},
tickOptions : {
formatString : '%b %#d, %H:%M:%S',
angle : 60
}
},
yaxis : {
rendererOptions : {
tickRenderer : $.jqplot.CanvasAxisTickRenderer
},
tickOptions : {
fontSize : '10pt',
fontFamily : 'Tahoma',
angle : 30
}
}
};
}
ManagedBean
barModel.setExtender("customExtender");
I have a JSF line chart that uses PrimeFaces. I want to add labels to both x-axis & y-axis eg Year & Populations. I have used showLabel:true but its not working.The is code,
<p:lineChart id="chart" value="#{chartBean.linearModel}" xaxisAngle="-90">
<f:convertDateTime pattern="d-M-yyyy"/>
</p:lineChart>
function phaseChartExt() {
this.cfg.axes = {
xaxis: {
renderer: $.jqplot.DateAxisRenderer,
rendererOptions: { tickRenderer: $.jqplot.CanvasAxisTickRenderer },
tickOptions: {
showLabel:true,
showGridline: true,
formatString: '%H:%M',
angle: -90
}
}
yaxis: {
renderer: $.jqplot.DateAxisRenderer,
rendererOptions: { tickRenderer: $.jqplot.CanvasAxisTickRenderer },
tickOptions: {
showLabel:true,
showGridline: true,
formatString: '%H:%M',
angle: -90
}
}
}
}
Try to use
xaxisLabel="Year" yaxisLabel="Populations"
<p:lineChart id="chart" value="#{chartBean.linearModel}" xaxisAngle="-90" xaxisLabel="Year" yaxisLabel="Populations">
<f:convertDateTime pattern="d-M-yyyy"/>
</p:lineChart>
I have a problem with JQuery Flot chart. It doesn't show the bar chart (data_campaigns) at all but the (data_campaigns2) shows up just fine.
I would also like to know how to show data from both charts in tooltip. Now the tooltip is just showing random X and Y variables but I would like it show the amount of clicks.
//Chart - Campaigns
$(function () {
var data_campaigns = [
[1359766800,8],[1359853200,4],[1359939600,11],[1360026000,11],
[1360112400,15],[1360198800,12],[1360285200,16],[1360371600,7],
[1360458000,9],[1360544400,6],[1360630800,13],[1360717200,12],
[1360803600,6],[1360890000,13],[1360976400,3],[1361062800,9],
[1361149200,18],[1361235600,18],[1361322000,12],[1361408400,14],
[1361494800,7],[1361581200,5],[1361667600,3],[1361754000,9],
[1361840400,15],[1361926800,14],[1362013200,4],[1362099600,0],
[1362186000,0],[1362272400,0]];
var data_campaigns2 = [
[1359766800,8],[1359853200,4],[1359939600,11],[1360026000,11],
[1360112400,15],[1360198800,12],[1360285200,16],[1360371600,7],
[1360458000,9],[1360544400,6],[1360630800,13],[1360717200,12],
[1360803600,6],[1360890000,13],[1360976400,3],[1361062800,9],
[1361149200,18],[1361235600,18],[1361322000,12],[1361408400,14],
[1361494800,7],[1361581200,5],[1361667600,3],[1361754000,9],
[1361840400,15],[1361926800,14],[1362013200,4],[1362099600,0],
[1362186000,0],[1362272400,0]];
var plot = $.plot($("#placeholder"),
[ { data: data_campaigns,color:"rgba(0,0,0,0.2)", shadowSize:0,
bars: {
show: true,
lineWidth: 0,
fill: true,
fillColor: { colors: [ { opacity: 1 }, { opacity: 1 } ] }
}
} ,
{ data: data_campaigns2,
color:"rgba(255,255,255, 0.4)",
shadowSize:0,
lines: {show:true, fill:false}, points: {show:false},
bars: {show:false},
}
],
{
series: {
bars: {show:true, barWidth: 0.6}
},
grid: { show:false, hoverable: true, clickable: false, autoHighlight: true, borderWidth:0 },
yaxis: {
min: 0
},
xaxis: {
tickDecimals: 0
}
});
function showTooltip(x, y, contents) {
console.log(x+","+y);
var d = new Date(contents *1000);
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
$('<div id="tooltip"><div class="date">'+curr_date + "." + curr_month + "." + curr_year+'<\/div><div class="title text_color_3">'+x+'%<\/div> <div class="description text_color_3">CTR<\/div><div class="title ">'+y+'<\/div><div class="description">Clicks<\/div><\/div>').css( {
position: 'absolute',
display: 'none',
top: y - 125,
left: x - 40,
border: '0px solid #ccc',
padding: '2px 6px',
'background-color': '#fff',
opacity: 10
}).appendTo("body").fadeIn(200);
}
var previousPoint = null;
$("#placeholder").bind("plothover", function (event, pos, item) {
$("#x").text(pos.x.toFixed(2));
$("#y").text(pos.y.toFixed(2));
if (item) {
if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex;
$("#tooltip").remove();
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(2);
showTooltip(item.pageX, item.pageY,
x);
}
}
});
1) the x, y parameters in function showTooltip(x, y, contents)
are actually not x,y values from your chart, bude x, y coordinates where to place the tooltip at. The tooltip value (text displayed in tooltip) is in parameter contents, so instead of:
$('<div id="tooltip"><div class="date">'+curr_date + "." + curr_month + "." + curr_year+'<\/div><div class="title text_color_3">'+x+'%<\/div> <div class="description text_color_3">CTR<\/div><div class="title ">'+y+'<\/div><div class="description">Clicks<\/div><\/div>').css( {...
you need something like this:
$('<div id="tooltip">' + contents + '<\/div>').css({...
with contents variable filled with whatever you need.
2) you need to set mode option
xaxis: {
mode: 'time',
...
}
and play a bit with the options to display bars. in the jsfiddle example below i set the lineWidth: 10 and changed some colors
3) Blake's advice about the timestamps is right. (not solving the bar visibility, but solving the correct x axis date values), when populating the data array, multiply them by 1000 to be displayed correctly
here is the jsFiddle, have a look at it