Highcharts tooltipformat for each series? - asp.net-mvc

I m using dotnetHightcharts
BasicColumnChart
public static Highcharts BasicColumnChart(Series[] Series, string[] Categories, string Title = "", string SubTitle = "", string XAxisTitle = "", string YAxisTitle = "", string ToolTipFormat = "", string YAxisLabel = "")
{
Highcharts chart = new Highcharts("chart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column, Height = 300 })
.SetTitle(new Title { Text = Title })
.SetSubtitle(new Subtitle { Text = SubTitle })
.SetXAxis(new XAxis { Categories = Categories })
.SetYAxis(new YAxis
{
Min = 0,
Title = new YAxisTitle { Text = YAxisTitle },
Labels = new YAxisLabels
{
Formatter = #"function() { return this.value +' " + YAxisLabel + "';}"
}
})
.SetLegend(new Legend
{
Layout = Layouts.Vertical,
VerticalAlign = VerticalAligns.Top,
Align = HorizontalAligns.Right,
Shadow = true,
BackgroundColor = ColorTranslator.FromHtml("#FFFFFF"),
Floating = true
})
.SetTooltip(new Tooltip { Formatter = #"function() { return ''+ this.x +' - '+ this.y +' " + ToolTipFormat + "'; }" })
.SetPlotOptions(new PlotOptions
{
Column = new PlotOptionsColumn
{
PointPadding = 0.2,
BorderWidth = 0
}
})
.SetSeries(Series);
return chart;
}
I can set same tooltipFormat for every series with following line:
.SetTooltip(new Tooltip { Formatter = #"function() { return ''+ this.x +' - '+ this.y +' " + ToolTipFormat + "'; }" })
Above code output : date - value tooltipFormat
But I want to show different format for each series. Why do I want this, Because I want to show different data that has different unit, for example:
value[0] = 220 V //Volt
value[1] = 5 A //Ampere
...
value[15] = 1200 kW //Kilowatt
there are a lot of example about this topic but, these examples only change text. My data views like this:
NAME VALUE UNIT
Volt 220 V
Ampere 5 A
....
I want to change tooltipFormat for each series dynamicly. I want to add UNIT field dynamicly. How can I do this?
Thanks.

You can add custom attribute in series object:
series:{name:'series1',unit:'%'}
and can access it in tooltip formatter function:
this.series.options.unit

You can do this directly in a series:
http://api.highcharts.com/highcharts#plotOptions.series.tooltip.valueSuffix
For example, this is a series:
series: [{
data: [10, 20, 30],
type: "line",
name: "Percentage",
tooltip: { valueSuffix: "%", }
},]

Related

Avoid overlapping of data points and provide tool tip for the same in advanced accessible highcharts

Below are my questions
I am using advanced accessible chart in high charts and I have customized few data points to be circle . Since the data for these points is almost in the same range , it's causing the data points to overlap . How do I prevent this?
the tooltip provided for these customized data points(circle) are not showing up . How to get the tool tip displayed.
I have been pondering a lot but none of the solution is working . Any help is appreciated.
Thanks.
enter image description here
function DrawRemoteRequestPSRChart(seriesData, yAxisCategories) {
Highcharts.seriesType('lowmedhigh', 'boxplot', {
keys: ['low', 'high'],
}, {
drawPoints: function () {
var series = this;
this.points.forEach(function (point) {
var graphic = point.graphic,
verb = graphic ? 'animate' : 'attr',
shapeArgs = point.shapeArgs,
width = 0,
left = Math.floor(shapeArgs.x) + 0.5,
right = left + width,
crispX = left + Math.round(width / 2) + 0.5,
highPlot = Math.floor(point.highPlot) + 0.5,
lowPlot = Math.floor(point.lowPlot) +
0.5 - (point.low === 0 ? 1 : 0);
if (point.low == point.high) {
highPlot = lowPlot - 20;
}
else if (lowPlot - highPlot < 20) {
highPlot = lowPlot - 20;
}
if (point.isNull) {
return;
}
if (!graphic) {
point.graphic = graphic = series.chart.renderer
.path('point')
.add(series.group);
} else {
console.log('graphic');
}
if (Math.floor(point.lowPlot) == Math.floor(point.highPlot)) {
graphic.attr({
stroke: "#FFCD11",
"stroke-width": 2,
});
//console.log('point.lowPlot & highplot ' + point.lowPlot, point.highPlot )
}
else {
graphic.attr({
stroke: "lime",
"stroke-width": 4,
"borderColor": "black"
});
}
graphic[verb]({
d: [
'M', left, highPlot,
'H', right,
'M', left, lowPlot,
'H', right,
'M', crispX, highPlot,
'V', lowPlot
]
});
});
}
});
var chart = Highcharts.chart('GraphHere', {
chart: {
type: 'lowmedhigh',
inverted: true,
},
credits: {
enabled: false
}, legend: {
enabled: false
},
title: {
text: ''
},
tooltip: {
useHTML: true,
shared: false,
formatter: function () {
if (this.point.options.low != this.point.options.high) {
return this.point.category + ': <br>PSR Request Time: ' + new Date(this.point.options.low).toGMTString() +
' <br>PSR Received Time: ' + new Date(this.point.options.high).toGMTString();
} else {
return this.point.category + ': <br>PSR Request Time: ' + new Date(this.point.options.low).toGMTString() +
' <br>PSR Received Time: N/A';
}
},
pointFormatter: function () {
if (this.point.options.low != this.point.options.high) {
return this.point.category + ': <br>PSR Request Time: ' + new Date(this.point.options.low).toGMTString() +
' <br>PSR Received Time: ' + new Date(this.point.options.high).toGMTString();
} else {
return this.point.category + ': <br>PSR Request Time: ' + new Date(this.point.options.low).toGMTString() +
' <br>PSR Received Time: N/A';
}
}
},
xAxis: [{
categories: yAxisCategories,
crosshair: true,
gridLineWidth: 1,
}],
yAxis: {
type: 'datetime',
gridLineWidth: 0
},
plotOptions: {
column: {
pointWidth: 10
},
series: {
stickyTracking: true,
whiskerWidth: 2,
grouping: true
}
},
series: seriesData
});
var seriesidexlength = chart.series.length;
for (var seriesidex = 0; seriesidex < seriesidexlength; seriesidex++) {
var dataindexlength = chart.series[seriesidex].data.length;
for (var dataindex = 0; dataindex < dataindexlength; dataindex++) {
var fromdate = new Date(chart.series[seriesidex].data[dataindex].low);
var todate = new Date(chart.series[seriesidex].data[dataindex].high);
if (fromdate.getDate() == todate.getDate()) {
//if (chart.series[seriesidex].data[dataindex].low == chart.series[seriesidex].data[dataindex].high) {
if (chart.series[seriesidex].data[dataindex].graphic != undefined) {
var obj = chart.series[seriesidex].data[dataindex].graphic.renderer.symbol('circle', chart.series[seriesidex].data[dataindex].plotX, chart.series[seriesidex].data[dataindex].plotY, 5).attr({ fill: 'orange', pointwidth: 10 }).add(chart.series[seriesidex].group);
chart.series[seriesidex].data[dataindex].graphic.destroy();
chart.series[seriesidex].data[dataindex].graphic = obj;
//console.log("same date - " + chart.series[seriesidex].data[dataindex].plotX, chart.series[seriesidex].data[dataindex].plotY)
} else {
//console.log("undefined"+seriesidex, dataindex)
}
//}
} else {
//console.log("not same" + seriesidex, dataindex)
}
}
}
chart.container.onmousedown = null;
chart.container.onclick = null;
$('.highcharts-yaxis').hide();
}

why x-axis showing alternate months rather every month?

Following is my highcharts config, can you help me to why my alternate months are coming, and if i want to show every month, how do i do it ? Also, to change width of the bar if showing every month.
Highcharts.chart("energy_chart", {
chart: {
type: "column",
spacingBottom: 15,
spacingTop: 10,
spacingLeft: 10,
spacingRight: 10,
backgroundColor: "#f2f2f2",
events: {
load: function() {
var fin = new Date();
var finDate = fin.getDate();
var finMonth = fin.getMonth();
var finYear = fin.getFullYear();
var ini = new Date();
ini.setFullYear(ini.getFullYear() - 1);
var iniDate = ini.getDate();
var iniMonth = ini.getMonth();
var iniYear = ini.getFullYear();
if (this.yAxis[0].dataMax == 0) {
this.yAxis[0].setExtremes(null, 1);
}
//this.yAxis.set
console.log(new Date(Date.UTC(iniYear, iniMonth, iniDate)))
console.log(new Date(Date.UTC(finYear, finMonth, finDate)))
this.xAxis[0].setExtremes(
Date.UTC(iniYear, iniMonth, iniDate),
Date.UTC(finYear, finMonth, finDate)
);
},
drilldown: function(e) {
console.log('drilldown')
var charts_this = this;
var inidrillDate = new Date(e.point.x);
setTimeout(function() {
inidrillDate.setDate(0);
inidrillDate.setMonth(inidrillDate.getMonth());
var DateinidrillDate = inidrillDate.getDate();
var MonthinidrillDate = inidrillDate.getMonth();
var YearinidrillDate = inidrillDate.getFullYear();
var findrillDate = inidrillDate;
findrillDate.setMonth(findrillDate.getMonth() + 1);
findrillDate.setDate(findrillDate.getDate() - 1);
var DatefindrillDate = findrillDate.getDate();
var MonthfindrillDate = findrillDate.getMonth();
var YearfindrillDate = findrillDate.getFullYear();
console.log(Date.UTC(
YearinidrillDate,
MonthinidrillDate,
DateinidrillDate
))
console.log(Date.UTC(
YearfindrillDate,
MonthfindrillDate,
DatefindrillDate
))
charts_this.xAxis[0].setExtremes(
Date.UTC(
YearinidrillDate,
MonthinidrillDate,
DateinidrillDate
),
Date.UTC(
YearfindrillDate,
MonthfindrillDate,
DatefindrillDate
)
);
if (charts_this.yAxis[0].dataMax === 0) {
charts_this.yAxis[0].setExtremes(null, 1);
}
}, 0);
}
}
},
title: {
text: '<p className="energy_gen">Energy Generated</p>'
},
exporting: { enabled: false },
xAxis: {
type: "datetime",
labels: {
step: 1
},
dateTimeLabelFormats: {
day: "%e"
}
},
yAxis: {
title: {
text: "kWh"
}
},
credits: {
enabled: false
},
plotOptions: {
series: {
cursor: "pointer",
dataLabels: {
enabled: true,
format: "{point.y}"
},
color: "#fcd562",
point:{
events:{
click:function(event){
}
}
}
}
}
},
tooltip: {
formatter: function() {
if (this.point.options.drilldown) {
return (
"Energy generated: <b> " +
this.y +
"</b> kWh " +
"<br>" +
Highcharts.dateFormat("%b %Y", new Date(this.x))
);
} else {
return (
"Energy generated: <b> " +
this.y +
"</b> kWh " +
"<br>" +
Highcharts.dateFormat("%e %b %Y", new Date(this.x))
);
}
}
},
series: [{'data':obj.data,'name':obj.name,"color":"#4848d3"}],
drilldown: {
series: obj.data
}
});
Also, attaching the screenshot of the rendered highchart with drilldown.
,
Now drilldown graph(same sort a issue)
EDIT:
It turnsout to be a zoom issue, i.e if i zoomout enough, then it shows all points. So, how to show every point without zooimg out .
Working fiddle:
http://jsfiddle.net/gkumar77/w9ngp63u/5/
Use tickInterval property and set it to one month:
xAxis: {
type: "datetime",
tickInterval: 30 * 24 * 3600 * 1000,
labels: {
step: 1
},
dateTimeLabelFormats: {
day: "%e"
}
}
Live demo: http://jsfiddle.net/BlackLabel/q5coany2/
API: https://api.highcharts.com/highcharts/xAxis.tickInterval

Highcharts : compare current value/previous value to get the rate

let me know how to get the difference between my current value "this.y.toLocalString()" and the previous value ? I would like to know the rate of change between this 2 values.
var x = document.getElementById("people2").selectedIndex;
var y = document.getElementsByTagName("option")[x].id;
//Charts
var db = data.dataevolution[x]
$('#container').highcharts({
chart:{
type:'column',
},
xAxis:{
categories: [
'2005','2006','2007','2008',
]
},
yAxis: {
min: 0,
title: {
text: 'Nombre'
},
labels: {
formatter: function() {
return this.value.toLocaleString();
}
}
},
tooltip: {
formatter: function() {
return 'Le nombre de <b>' + this.series.name + '</b> est de <b>' + this.y.toLocaleString() + '</b>, en '+ this.x +'<br>soit une évolution de ' ; }
},
series: [
{
name: [db.metier],
data: [db.annee2005,db.annee2006,db.annee2007,db.annee2008]
},
]
});
You can get the prevous point like:
tooltip: {
formatter: function() {
var prevPoint = this.point.x == 0 ? null : this.series.data[this.point.x - 1];
// do stuff with it
}
}
Here's a fiddle example.

Highcharts - combining column chart with scatter chart

I have one column chart, representing two categories on X-axis - ARRIVALS and DEPARTURES. Then, I have 4 series representing different commodities, one commodity per each category. This will end up in having 8 columns draw on the chart.
What I'm trying to do, is to add a scatter chart, representing one point per each bar ( a total of 8 points), but unfortunately, I only can add one point per category. Can this be implemented?
Here is the code that I used for this (it is MVC .NET):
Highcharts barChartArrivalsDepartures = new Highcharts("ArrivalsDepartures")
.InitChart(new Chart
{
BorderColor = Color.Black,
BorderRadius = 0,
BorderWidth = 2
})
.SetTitle(new Title { Text = "Arrivals and Departures" })
.SetXAxis(new XAxis { Categories = new[] { "Arrivals", "Departures"} })
.SetYAxis(new YAxis
{
Min = 0,
Title = new YAxisTitle { Text = "Count [t]" }
})
.SetLegend(new Legend
{
Layout = Layouts.Vertical,
Align = HorizontalAligns.Left,
VerticalAlign = VerticalAligns.Top,
X = 290,
Y = 0,
Floating = true,
BackgroundColor = new BackColorOrGradient(new Gradient
{
LinearGradient = new[] { 0, 0, 0, 400 },
Stops = new object[,]
{
{ 0, Color.FromArgb(13, 255, 255, 255) },
{ 1, Color.FromArgb(13, 255, 255, 255) }
}
}),
Shadow = true
})
.SetTooltip(new Tooltip { Formatter = #"function() { return ''+ this.x +': '+ this.y +' tones'; }" })
.SetSeries(new[]
{
new Series { Type = ChartTypes.Column, Name = "Coal", Data = new Data(new object[] { 49.9, 71.5 }), PlotOptionsColumn = new PlotOptionsColumn { PointPadding = 0.2, BorderWidth = 0 } },
new Series { Type = ChartTypes.Column, Name = "Magnetite", Data = new Data(new object[] { 48.9, 38.8 }) , PlotOptionsColumn = new PlotOptionsColumn { PointPadding = 0.2, BorderWidth = 0 } },
new Series { Type = ChartTypes.Column, Name = "Iron Ore", Data = new Data(new object[] { 83.6, 78.8 }) , PlotOptionsColumn = new PlotOptionsColumn { PointPadding = 0.2, BorderWidth = 0 } },
new Series { Type = ChartTypes.Column, Name = "Grain", Data = new Data(new object[] { 42.4, 33.2 }) , PlotOptionsColumn = new PlotOptionsColumn { PointPadding = 0.2, BorderWidth = 0 } },
new Series
{
Type = ChartTypes.Scatter,
Name = "Target Coal",
Color = ColorTranslator.FromHtml("#FF0000"),
//Data = new Data(new object[] { new object[] {40}, new object[] {80}}),
Data = new Data(new object[] { 15, 50} ),
PlotOptionsScatter = new PlotOptionsScatter
{
Marker = new PlotOptionsScatterMarker
{
Symbol = "diamond",
Radius = 3
}
}
}
})
It's not solution in .net but general idea is to calculate x-value for each of scatter points, for example: http://jsfiddle.net/3bQne/206/
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'column',
},
xAxis: {
categories: ['+', '-']
},
series: [{
data: [1,2],
id: 'first',
stack: 0
}, {
data: [3,5],
id: 'second',
stack: 0
}, {
data: [[-0.15,1],[0.85,2]],
linkedTo: 'first',
type: 'scatter'
}, {
data: [[0.15,3],[1.15,5]],
linkedTo: 'second',
type: 'scatter'
}]
});

highcharts example for using data from database with mvc

Does anyone have a simple example of populating a Highchart with data from a MS SQL DB using DotNet.Highcarts?
I have the demo working with static data
Highcharts chart = new Highcharts("chart")
.SetCredits(new Credits { Enabled = false })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetTitle(new Title { Text = "Membership Overview" })
.SetXAxis(new XAxis { Categories = new[] { "Paid Members", "Active Members", "Retained Members", "New Members", "Lapsed Members" } })
.SetYAxis(new YAxis
{
Min = 0,
Title = new YAxisTitle { Text = "Total Members" }
})
.SetTooltip(new Tooltip { Formatter = "function() { return ''+ this.series.name +': '+ this.y +''; }" })
.SetPlotOptions(new PlotOptions { Bar = new PlotOptionsBar { Stacking = Stackings.Normal } })
.SetSeries(new[]
{
new Series { Name = "Total", Data = new Data(new object[] { 441, 441, 22, 30, 610 }) }
});
How can I change the Series to accept data from my DB?
Assuming I need to connect to DB like so:
var newcustomer = db.Customer;
Then do something like this:
new Series
{
Name = "Total",
Data = new Data(newcustomer.Select(x => ............
Any help would be appreciated!
For anyone else having a similar issue...
var paidmembers = (from c in db.Customer
where c.CustomerStatusID == 1
select c).Count();
var activemembers = (from c in db.Customer
where c.CustomerStatusID == 2
select c).Count();
new Series {
Name = "Category",
Data = new Data(new object[] {paidmembers, activemembers})
}

Resources