Hi,
How to set dynamic categories (SetXAxis) in DotNet.Highcharts
I am trying to set dynamnic categores from DB. It's throwing an error:
CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<object>[]' to 'string[]' Source Error:
|
Line 153: .SetXAxis(new XAxis
Line 154: {
Line 155: Categories = new[] { chartValues3 }
Line 156: })
Line 157:
|
can you please help? thank you!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNet.Highcharts.Enums;
using DotNet.Highcharts.Helpers;
using DotNet.Highcharts.Options;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Drawing;
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Render_Chart2();
}
protected void Render_Chart2()
{
// define the connection string
string constring = WebConfigurationManager.ConnectionStrings["ERDASHConnectionString"].ToString();
// Declare the SQL connection
SqlConnection myConn = new SqlConnection(constring);
// and add a query string for retrieving the data.
string commandText = "SELECT id, Adult_Patients, AtDoor FROM [Volume_ILI30]";
SqlCommand myComm = new SqlCommand(commandText, myConn);
// Open the connection
myConn.Open();
// and execute the query
SqlDataReader reader = myComm.ExecuteReader();
Object[] chartValues = new Object[30]; // declare an object for the chart rendering
if (reader.HasRows)
{
while (reader.Read())
{
chartValues[(int)reader.GetValue(0) - 1] = reader.GetValue(1);
// minus 1 because the array starts from 0, whenever the months start from 1
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close(); // close the reader
//-------------------------------------------------------------------------------
string constring2 = WebConfigurationManager.ConnectionStrings["ERDASHConnectionString"].ToString();
SqlConnection myConn2 = new SqlConnection(constring2);
string commandText2 = "SELECT id,Ped_Patients, AtDoor FROM [Volume_ILI30]";
SqlCommand myComm2 = new SqlCommand(commandText2, myConn2);
myConn2.Open();
SqlDataReader reader2 = myComm2.ExecuteReader();
Object[] chartValues2 = new Object[30]; // declare an object for the chart rendering
if (reader2.HasRows)
{
while (reader2.Read())
{
// GetValue() returns the data row from the query
// So:
// GetValue(0) will contain the month number [<em>month(eaten_Pizza) as Mese</em>]
// GetValue(1) will contain the number of eaten pizzas [<em>count(eaten_Pizza)</em>]
chartValues2[(int)reader2.GetValue(0) - 1] = reader2.GetValue(1);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader2.Close(); // close the reader
//-------------------------------------------------------------------------------
//Categories
string constring3 = WebConfigurationManager.ConnectionStrings["ERDASHConnectionString"].ToString();
SqlConnection myConn3 = new SqlConnection(constring3);
string commandText3 = "SELECT id, AtDoor FROM [Volume_ILI30]";
SqlCommand myComm3 = new SqlCommand(commandText3, myConn3);
myConn3.Open();
SqlDataReader reader3 = myComm3.ExecuteReader();
List<object> chartValues3 = new List<object>();
if (reader3.HasRows)
{
while (reader3.Read())
chartValues3.Add(reader3.GetValue(1));
}
else
Console.WriteLine("No rows found.");
reader3.Close();
//-------------------------------------------------------------------------------
// Declare the HighCharts object
DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart2")
.InitChart(new Chart
{
DefaultSeriesType = ChartTypes.Line,
//MarginRight = 130,
//MarginBottom = 25,
ClassName = "chart"
})
.SetPlotOptions(new PlotOptions
{
Line = new PlotOptionsLine
{
Stacking = Stackings.Normal,
DataLabels = new PlotOptionsLineDataLabels
{
Enabled = true,
Color = Color.Black,
}
}
})
.SetXAxis(new XAxis
{
Categories = new[] { chartValues3 }
})
.SetYAxis(new YAxis
{
Min = 0,
Title = new YAxisTitle { Text = "Patients" },
StackLabels = new YAxisStackLabels
{
Enabled = true,
//X=20,
Y = -30,
Style = "fontWeight: 'bold', color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'"
}
})
.SetTooltip(new Tooltip
{
Formatter = @"function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y +'°C';
}"
})
.SetLegend(new Legend
{
//Layout = Layouts.Horizontal,
//Align = HorizontalAligns.Right,
//VerticalAlign = VerticalAligns.Bottom,
//X = -100,
//Y = 40,
Floating = false,
BackgroundColor = new BackColorOrGradient(ColorTranslator.FromHtml("#FFFFFF")),
BorderColor = ColorTranslator.FromHtml("#CCC"),
BorderWidth = 1,
Shadow = false
})
.SetSeries(new[]
{
new Series
{
Name = "ADULTS",
Color = ColorTranslator.FromHtml("#4572A7"),
Data = new Data(chartValues) // Here we put the dbase data into the chart
},
new Series
{
Name = "PEDS",
Color = ColorTranslator.FromHtml("#89A54E"),
Data = new Data(chartValues2) // Here we put the dbase data into the chart
}
});
chrtMyChart2.Text = chart.ToHtmlString(); // Let's visualize the chart into the webform.
}
}