Gallery
JS Average Rate
<%@ Page Language="C#" Description="dotnetCHARTING Component" %>
<%@ Register TagPrefix="dotnet" Namespace="dotnetCHARTING" Assembly="dotnetCHARTING" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// This sample deomonstrates a computed line series with muted source lines for context.
// Set he chart size.
Chart.Width = 740;
Chart.Height = 400;
// Set the title.
Chart.TitleBox.Label.Text = "Average US Unemployment Rate";
Chart.Debug = false;
Chart.DefaultElement.ToolTip = "";
Chart.ChartArea.ClearColors();
Chart.LegendBox.Visible = false;
Chart.DefaultSeries.LegendEntry.Visible = false;
Chart.Palette = new Color[] { };
// Set the directory where the related JSC files will be created and stored.
Chart.TempDirectory = "temp";
Chart.DefaultElement.Marker.Visible = false;
Chart.DefaultSeries.Type = SeriesType.Spline;
Chart.DefaultSeries.Line.Width = 1;
Chart.DefaultSeries.Line.Color = Color.LightGray;
Chart.DefaultElement.FocusGlow.Width = 0;
// Enable JSCharting
Chart.JS.Enabled = true;
Chart.XAxis.Crosshair = new AxisTick();
Chart.XAxis.Scale = Scale.Time;
Chart.XAxis.FormatString = "yyyy";
Chart.XAxis.DefaultTick.GridLine.Color = Color.LightGray;
Chart.YAxis.DefaultTick.GridLine.Color = Color.LightGray;
Chart.YAxis.DefaultTick.Label.Text = "%value%";
Chart.YAxis.ScaleRange.ValueLow = 2;
Chart.YAxis.ScaleRange.ValueHigh = 14;
DataEngine de = new DataEngine();
de.ChartObject = Chart; // Necessary to view any errors the dataEngine may throw.
de.Data = "./../../data/resources/laborForceUs.csv";
de.DataFields = "yAxis=unemployed,xAxis=year,splitby=state,labor_force";//cvs must have header
SeriesCollection sc = de.GetSeries();
SeriesCollection orgs = new SeriesCollection();
//calculae average
int count = sc.Count;
for (int i = 0; i < sc.Count; i++)
{
if (string.IsNullOrEmpty(sc[i].Name))
continue;
ElementCollection ec = sc[i].GetActiveElements();
Series ser = new Series();
ser.Name = sc[i].Name;
foreach (Element el in ec)
{
if (el.XValue < 1991)
continue;
Element newEl = new Element();
double rate = (el.YValue * 100) / double.Parse(el.CustomAttributes["labor_force"].ToString());
newEl.YValue = rate;
newEl.XDateTime = new DateTime((int)el.XValue, 1, 1);
ser.Elements.Add(newEl);
}
orgs.Add(ser);
}
Series average = new Series();
average.Name = "Average Rate'";
average.Type = SeriesType.Spline;
average.Line.Width = 3;
average.Line.Color = Color.RoyalBlue;
average.DefaultElement.ToolTip = "<hr>Averate rate in US: <b>{%yValue:n2}%</b><br>Min Rate: <b>%min%</b><br>Max Rate: <b>%max%</b>";
int elementCount = orgs[0].Elements.Count;
for (int i = 0; i < elementCount; i++)
{
string maxName = "";
string minName = "";
double maxRate = 0;
double minRate = 100;
double avg = 0;
for (int j = 0; j < orgs.Count; j++)
{
if (orgs[j].Elements[i].YValue > maxRate)
{
maxRate = orgs[j].Elements[i].YValue;
maxName = orgs[j].Name;
}
if (orgs[j].Elements[i].YValue < minRate)
{
minRate = orgs[j].Elements[i].YValue;
minName = orgs[j].Name;
}
avg += orgs[j].Elements[i].YValue;
}
Element el = new Element();
el.XDateTime = new DateTime(1991 + i, 1, 1);
el.YValue = Math.Round(avg / orgs.Count, 2);
el.CustomAttributes.Add("min", minName + " " + Math.Round(minRate, 2));
el.CustomAttributes.Add("max", maxName + " " + Math.Round(maxRate, 2));
average.Elements.Add(el);
}
Chart.SeriesCollection.Add(orgs);
Chart.SeriesCollection.Add(average);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Gallery Sample</title></head>
<body>
<div align="center">
<dotnet:Chart ID="Chart" runat="server" />
</div>
</body>
</html>
<%@ Page Language="vb" Description="dotnetCHARTING Component" %>
<%@ Register TagPrefix="dotnet" Namespace="dotnetCHARTING" Assembly="dotnetCHARTING" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' This sample deomonstrates a computed line series with muted source lines for context.
' Set he chart size.
Chart.Width = 740
Chart.Height = 400
' Set the title.
Chart.TitleBox.Label.Text = "Average US Unemployment Rate"
Chart.Debug = False
Chart.DefaultElement.ToolTip = ""
Chart.ChartArea.ClearColors()
Chart.LegendBox.Visible = False
Chart.DefaultSeries.LegendEntry.Visible = False
Chart.Palette = New Color() { }
' Set the directory where the related JSC files will be created and stored.
Chart.TempDirectory = "temp"
Chart.DefaultElement.Marker.Visible = False
Chart.DefaultSeries.Type = SeriesType.Spline
Chart.DefaultSeries.Line.Width = 1
Chart.DefaultSeries.Line.Color = Color.LightGray
Chart.DefaultElement.FocusGlow.Width = 0
' Enable JSCharting
Chart.JS.Enabled = True
Chart.XAxis.Crosshair = New AxisTick()
Chart.XAxis.Scale = Scale.Time
Chart.XAxis.FormatString = "yyyy"
Chart.XAxis.DefaultTick.GridLine.Color = Color.LightGray
Chart.YAxis.DefaultTick.GridLine.Color = Color.LightGray
Chart.YAxis.DefaultTick.Label.Text = "%value%"
Chart.YAxis.ScaleRange.ValueLow = 2
Chart.YAxis.ScaleRange.ValueHigh = 14
Dim de As DataEngine = New DataEngine()
de.ChartObject = Chart ' Necessary to view any errors the dataEngine may throw.
de.Data = "./../../data/resources/laborForceUs.csv"
de.DataFields = "yAxis=unemployed,xAxis=year,splitby=state,labor_force" 'cvs must have header
Dim sc As SeriesCollection = de.GetSeries()
Dim orgs As SeriesCollection = New SeriesCollection()
'calculae average
Dim count As Integer = sc.Count
For i As Integer = 0 To sc.Count - 1
If String.IsNullOrEmpty(sc(i).Name) Then
Continue For
End If
Dim ec As ElementCollection = sc(i).GetActiveElements()
Dim ser As Series = New Series()
ser.Name = sc(i).Name
For Each el As Element In ec
If el.XValue < 1991 Then
Continue For
End If
Dim newEl As Element = New Element()
Dim rate As Double = (el.YValue * 100) / Double.Parse(el.CustomAttributes("labor_force").ToString())
newEl.YValue = rate
newEl.XDateTime = New DateTime(CInt(Fix(el.XValue)), 1, 1)
ser.Elements.Add(newEl)
Next el
orgs.Add(ser)
Next i
Dim average As Series = New Series()
average.Name = "Average Rate'"
average.Type = SeriesType.Spline
average.Line.Width = 3
average.Line.Color = Color.RoyalBlue
average.DefaultElement.ToolTip = "<hr>Averate rate in US: <b>{%yValue:n2}%</b><br>Min Rate: <b>%min%</b><br>Max Rate: <b>%max%</b>"
Dim elementCount As Integer = orgs(0).Elements.Count
For i As Integer = 0 To elementCount - 1
Dim maxName As String = ""
Dim minName As String = ""
Dim maxRate As Double = 0
Dim minRate As Double = 100
Dim avg As Double = 0
For j As Integer = 0 To orgs.Count - 1
If orgs(j).Elements(i).YValue > maxRate Then
maxRate = orgs(j).Elements(i).YValue
maxName = orgs(j).Name
End If
If orgs(j).Elements(i).YValue < minRate Then
minRate = orgs(j).Elements(i).YValue
minName = orgs(j).Name
End If
avg += orgs(j).Elements(i).YValue
Next j
Dim el As Element = New Element()
el.XDateTime = New DateTime(1991 + i, 1, 1)
el.YValue = Math.Round(avg / orgs.Count, 2)
el.CustomAttributes.Add("min", minName & " " & Math.Round(minRate, 2))
el.CustomAttributes.Add("max", maxName & " " & Math.Round(maxRate, 2))
average.Elements.Add(el)
Next i
Chart.SeriesCollection.Add(orgs)
Chart.SeriesCollection.Add(average)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Gallery Sample</title></head>
<body>
<div align="center">
<dotnet:Chart ID="Chart" runat="server" />
</div>
</body>
</html>
- Sample FilenameJsAverageRate.aspx
- Version10.1
- Uses DatabaseNo