How to continue chart when empty points exist? - c#

using System.Web.UI.DataVisualization.Charting.Chart
My data is like so:
...but my chart does not draw lines between points of missing data:
As you can see, the 2nd column has a point at 51 on 7/09/2015 and then points are empty until 7/16/2015. I want the chart to draw a line from the point at 7/09/2015 to 7/16/2015.
How can I get this to work?
This is my existing Series construction:
var series = chart.Series[header] = new Series(header) {
BorderWidth = 2,
ChartArea = DataTable.TableName,
ChartType = SeriesChartType.FastLine,
Color = legendColors[header],
Enabled = true,
Font = new Font("Lucida Sans Unicode", 6f),
XValueMember = "Week",
YValueMembers = header
};
Update 1:
With #jstreet's answer, I am now getting a line drawn. But the value is being represented as if it were 0:
What is expected is the following where thick dashed lines should replace the lines representing a value of 0:
Update 2:
Modified the for-loop to manually add DataPoints to the series following #jstreet's example for add points in code-behind:
for (int column = 0; column < seriesHeaders.Length; column++) {
var header = seriesHeaders[column];
var series = chart.Series[header] = new Series(header) {
BorderWidth = 2,
ChartArea = DataTable.TableName,
ChartType = SeriesChartType.FastLine,
Color = legendColors[header],
Enabled = true,
Font = new Font("Lucida Sans Unicode", 6f),
XValueMember = "Week",
YValueMembers = header
};
series.EmptyPointStyle.Color = legendColors[header];
series.EmptyPointStyle.AxisLabel = "Empty";
DataTable.Rows
.OfType<DataRow>()
.Select(r => (double)r[header])
.ToList()
.ForEach(v => {
series.Points.Add(new DataPoint(series) {
IsEmpty = v == Double.NaN,
YValues = new double[] { v == Double.NaN ? 0 : v }
});
});
}
...chart still renders what is supposed to be an empty point as a 0 value.
Update 3:
Modified construction of DataPoints. Evidently v == Double.NaN does not evaluate properly:
DataTable.Rows
.OfType<DataRow>()
.Select(r => (double)r[header])
.ToList()
.ForEach(v => {
var isEmpty = Double.IsNaN(v);
var value = new double[] { v == Double.NaN ? 0 : v };
series.Points.Add(new DataPoint() {
IsEmpty = isEmpty,
YValues = value
});
});
... and I verified the Series.Points collection:

You need to define an EmptyPointStyle, as shown below:
<asp:Series Name="Series1" ChartType="Line" XValueType="DateTime">
<Points>
<asp:DataPoint XValue="42005" YValues="50" />
<asp:DataPoint XValue="42036" YValues="70" />
<asp:DataPoint XValue="42064" YValues="30" />
<asp:DataPoint IsEmpty="True" XValue="42095" YValues="0" />
<asp:DataPoint XValue="42156" YValues="60" />
<asp:DataPoint XValue="42186" YValues="40" />
</Points>
<EmptyPointStyle Color="Red" />
</asp:Series>
Result:
EDIT: I can also add points using code behind:
protected void Page_Load(object sender, EventArgs e)
{
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = false, XValue = DateTime.Now.AddDays(1).ToOADate(), YValues = new double[] { 50 } });
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = false, XValue = DateTime.Now.AddDays(2).ToOADate(), YValues = new double[] { 70 } });
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = true, XValue = DateTime.Now.AddDays(3).ToOADate(), YValues = new double[] { 0 } });
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = false, XValue = DateTime.Now.AddDays(4).ToOADate(), YValues = new double[] { 30 } });
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = false, XValue = DateTime.Now.AddDays(5).ToOADate(), YValues = new double[] { 60 } });
}
And i don't have the problem you're describing about not being able to set IsEmpty or getting 0's in your chart:

Changed one line following all the updates provided:
ChartType = SeriesChartType.Line, // was FastLine
So the Series construction looks like this now:
for (int column = 0; column < seriesHeaders.Length; column++) {
var header = seriesHeaders[column];
var series = chart.Series[header] = new Series(header) {
BorderWidth = 2,
ChartArea = DataTable.TableName,
ChartType = SeriesChartType.Line,
Color = legendColors[header],
Enabled = true,
Font = new Font("Lucida Sans Unicode", 6f),
XValueMember = "Week",
YValueMembers = header
};
series.EmptyPointStyle.Color = legendColors[header];
series.EmptyPointStyle.BorderWidth = 2;
DataTable.Rows
.OfType<DataRow>()
.Select(r => {
var value = (double)r[header];
return new {
isEmpty = Double.IsNaN(value),
yValue = new double[] { value },
xValue = DateTime.Parse(r["Week"].ToString()).ToOADate()
};
})
.ToList()
.ForEach(dp => {
var dataPoint = new DataPoint() {
IsEmpty = dp.isEmpty,
YValues = dp.yValue,
XValue = dp.xValue
};
series.Points.Add(dataPoint);
});
}
The chart is properly rendered:

Related

Livecharts showing "-100000000000000 μ" instead of -100 M in a StackedColumnSeries

When a value of -1e8 is added to a StackedColumnSeries in a SKCartesianChart, the value is labelled with -100000000000000 μ instead of -100 M as expected.
I suspect this is a bug in the prerelease version of livecharts. I've reported it, but thought to ask here in case anyone's seen the same problem and has a workaround.
var chart = new SKCartesianChart
{
Series = new List<ISeries>
{
new StackedColumnSeries<decimal> {Values = new [] {1e8m}},
new StackedColumnSeries<decimal> {Values = new [] {-1e8m}}, // -100000000000000 μ
}
};
Thanks for the report on Github, this is a bug, it is already fixed, and this fix fill be included in the next version of the library.
For now, you can build your own formatter in the YAxis:
var chart = new SKCartesianChart
{
Series = new List<ISeries>
{
new StackedColumnSeries<decimal> {Values = new [] {1e8m}},
new StackedColumnSeries<decimal> {Values = new [] {-1e8m}}
},
YAxes = new[]
{
new Axis
{
Labeler = value =>
{
var l = value == 0 ? 0 : (int)Math.Log10(Math.Abs(value));
if (l >= 6)
{
value /= Math.Pow(10, 6);
return value.ToString($"######0.####### M");
}
return value.ToString($"######0.#######");
}
}
}
};

Chartsjs (blazor) BarChart not alinged

Im not sure im doing something fairly simple wrong. Im getting below plot when using the below code. I was expecting to get the B values in its own column like you would in excel.
EDIT: I have added my config in the post also, if there are some properties that im missing
/Thomas
BarDataset<double> _barDataSet3 = new BarDataset<double>
{
Label = "A",
BackgroundColor = ColorUtil.RandomColorString(),
BorderWidth = 0,
HoverBackgroundColor = ColorUtil.RandomColorString(),
HoverBorderColor = ColorUtil.RandomColorString(),
HoverBorderWidth = 1,
BorderColor = "#ffffff"
};
_barChartConfig.Data.Labels.AddRange(new[] { "A"});
_barDataSet3.Add(2.6);
_barChartConfig.Data.Datasets.Add(_barDataSet3);
BarDataset<double> _barDataSet4 = new BarDataset<double>
{
Label = "B",
BackgroundColor = ColorUtil.RandomColorString(),
BorderWidth = 0,
HoverBackgroundColor = ColorUtil.RandomColorString(),
HoverBorderColor = ColorUtil.RandomColorString(),
HoverBorderWidth = 1,
BorderColor = "#ffffff"
};
_barChartConfig.Data.Labels.AddRange(new[] { "B" });
_barDataSet4.Add(4.5);
_barChartConfig.Data.Datasets.Add(_barDataSet4);
EDIT: My config - is there a property that im missing?:
_barChartConfig = new BarConfig
{
Options = new BarOptions
{
Title = new OptionsTitle
{
Display = true,
Text = "Simple Bar Chart"
},
Scales = new BarScales
{
XAxes = new List<CartesianAxis>
{
new BarCategoryAxis
{
BarPercentage = 0.5,
BarThickness = BarThickness.Flex
}
},
YAxes = new List<CartesianAxis>
{
new BarLinearCartesianAxis
{
Ticks = new LinearCartesianTicks
{
BeginAtZero = true
}
}
}
}
}
};

Styles in Excel using Open XML

I am quite new to C# & got stuck with a problem.
I am trying to add some style to the Excel using OpenXML, though it is not drawing borders, the fills (foreground & background colors) and alignment to the cells. I tried many times with different ways, though I am not able to do so.
The following code is not generating any error. Compiling successfully, however not giving the expected output.
Following is the actual output:
Actual Output
Following is the expected output:
Expected Output
The requirement is that only the top row to have this specific style, how can I achieve it?
Any guidance would be greatly appreciated.
Code snippet:
private void ExportDataSet()
{
WorkbookStylesPart wbsp = workbookPart.AddNewPart<WorkbookStylesPart>();
wbsp.Stylesheet = CreateStylesheet();
wbsp.Stylesheet.Save();
}
private static DocumentFormat.OpenXml.Spreadsheet.Stylesheet CreateStylesheet()
{
DocumentFormat.OpenXml.Spreadsheet.Stylesheet stylesheet1 = new DocumentFormat.OpenXml.Spreadsheet.Stylesheet();
#region Font
DocumentFormat.OpenXml.Spreadsheet.Fonts fonts = new DocumentFormat.OpenXml.Spreadsheet.Fonts() { Count = (DocumentFormat.OpenXml.UInt32Value)2U};
// FontID = 0 (The Default Font)
DocumentFormat.OpenXml.Spreadsheet.Font font0 = new DocumentFormat.OpenXml.Spreadsheet.Font();
DocumentFormat.OpenXml.Spreadsheet.FontSize fontSize0 = new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11D };
DocumentFormat.OpenXml.Spreadsheet.Color color0 = new DocumentFormat.OpenXml.Spreadsheet.Color() { Theme = (DocumentFormat.OpenXml.UInt32Value)1U };
DocumentFormat.OpenXml.Spreadsheet.FontName fontName0 = new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" };
font0.Append(fontSize0);
font0.Append(color0);
font0.Append(fontName0);
// FontID = 1
DocumentFormat.OpenXml.Spreadsheet.Font font1 = new DocumentFormat.OpenXml.Spreadsheet.Font();
DocumentFormat.OpenXml.Spreadsheet.FontSize fontSize1 = new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 12D };
DocumentFormat.OpenXml.Spreadsheet.FontName fontName1 = new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" };
DocumentFormat.OpenXml.Spreadsheet.Color color1 = new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("5B9BD5") };
DocumentFormat.OpenXml.Spreadsheet.Bold bold1 = new DocumentFormat.OpenXml.Spreadsheet.Bold() { Val = true };
// Foreground & Background not working
DocumentFormat.OpenXml.Spreadsheet.ForegroundColor foregroundColor1 = new DocumentFormat.OpenXml.Spreadsheet.ForegroundColor() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("FF00FF00") };
DocumentFormat.OpenXml.Spreadsheet.BackgroundColor backgroundColor1 = new DocumentFormat.OpenXml.Spreadsheet.BackgroundColor() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("FF00FF00") };
font1.Append(fontSize1);
font1.Append(fontName1);
font1.Append(color1);
font1.Append(bold1);
font1.Append(foregroundColor1);
font1.Append(backgroundColor1);
fonts.Append(font1);
int fontIndex = fonts.Count() - 1;
#endregion
#region CellBorders
DocumentFormat.OpenXml.Spreadsheet.Borders borders = new DocumentFormat.OpenXml.Spreadsheet.Borders() { Count = (DocumentFormat.OpenXml.UInt32Value)2U };
// Border ID = 0 (The Default Border)
DocumentFormat.OpenXml.Spreadsheet.Border border0 = new DocumentFormat.OpenXml.Spreadsheet.Border();
DocumentFormat.OpenXml.Spreadsheet.LeftBorder leftBorder0 = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder();
DocumentFormat.OpenXml.Spreadsheet.RightBorder rightBorder0 = new DocumentFormat.OpenXml.Spreadsheet.RightBorder();
DocumentFormat.OpenXml.Spreadsheet.TopBorder topBorder0 = new DocumentFormat.OpenXml.Spreadsheet.TopBorder();
DocumentFormat.OpenXml.Spreadsheet.BottomBorder bottomBorder0 = new DocumentFormat.OpenXml.Spreadsheet.BottomBorder();
border0.Append(leftBorder0);
border0.Append(rightBorder0);
border0.Append(topBorder0);
border0.Append(bottomBorder0);
// Border ID = 1
DocumentFormat.OpenXml.Spreadsheet.Border border1 = new DocumentFormat.OpenXml.Spreadsheet.Border();
DocumentFormat.OpenXml.Spreadsheet.LeftBorder leftBorder = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder() { Style = DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Thick };
DocumentFormat.OpenXml.Spreadsheet.RightBorder rightBorder = new DocumentFormat.OpenXml.Spreadsheet.RightBorder() { Style = DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Thick };
DocumentFormat.OpenXml.Spreadsheet.TopBorder topBorder = new DocumentFormat.OpenXml.Spreadsheet.TopBorder() { Style = DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Thick };
DocumentFormat.OpenXml.Spreadsheet.BottomBorder bottomBorder = new DocumentFormat.OpenXml.Spreadsheet.BottomBorder() { Style = DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Thick };
border1.Append(leftBorder);
border1.Append(rightBorder);
border1.Append(topBorder);
border1.Append(bottomBorder);
borders.Append(border1);
int borderIndex = borders.Count() - 1;
#endregion
#region Fill
DocumentFormat.OpenXml.Spreadsheet.Fills fills = new DocumentFormat.OpenXml.Spreadsheet.Fills() { Count = (DocumentFormat.OpenXml.UInt32Value)3U };
// Fill ID = 0 (The Default Fill)
DocumentFormat.OpenXml.Spreadsheet.Fill fill0 = new DocumentFormat.OpenXml.Spreadsheet.Fill();
DocumentFormat.OpenXml.Spreadsheet.PatternFill patternFill0 = new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = DocumentFormat.OpenXml.Spreadsheet.PatternValues.None }; // required, reserved by Excel
fill0.Append(patternFill0);
// Fill ID = 1 (The default fill of Gray)
DocumentFormat.OpenXml.Spreadsheet.Fill fill1 = new DocumentFormat.OpenXml.Spreadsheet.Fill();
DocumentFormat.OpenXml.Spreadsheet.PatternFill patternFill1 = new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = DocumentFormat.OpenXml.Spreadsheet.PatternValues.Gray125 }; // required, reserved by Excel
fill1.Append(patternFill1);
// Fill ID = 2
DocumentFormat.OpenXml.Spreadsheet.Fill fill2 = new DocumentFormat.OpenXml.Spreadsheet.Fill();
DocumentFormat.OpenXml.Spreadsheet.PatternFill patternFill2 = new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = DocumentFormat.OpenXml.Spreadsheet.PatternValues.Solid }; // customized Pattern value
DocumentFormat.OpenXml.Spreadsheet.ForegroundColor foregroundColorFill2 = new DocumentFormat.OpenXml.Spreadsheet.ForegroundColor() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("FF00FF00") };
DocumentFormat.OpenXml.Spreadsheet.BackgroundColor backgroundColorFill2 = new DocumentFormat.OpenXml.Spreadsheet.BackgroundColor() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("5B9BD500") };
patternFill2.Append(foregroundColorFill2);
patternFill2.Append(backgroundColorFill2);
fill2.Append(patternFill2);
fills.Append(fill2);
int fillIndex = fills.Count() - 1;
#endregion
#region Alignment
DocumentFormat.OpenXml.Spreadsheet.Alignment alignment = new DocumentFormat.OpenXml.Spreadsheet.Alignment()
{
Horizontal = DocumentFormat.OpenXml.Spreadsheet.HorizontalAlignmentValues.Left,
Vertical = DocumentFormat.OpenXml.Spreadsheet.VerticalAlignmentValues.Center
};
#endregion
#region Cell Style Formats & Cell Formats
DocumentFormat.OpenXml.Spreadsheet.CellFormats cellFormats0 = new DocumentFormat.OpenXml.Spreadsheet.CellFormats() { Count = (DocumentFormat.OpenXml.UInt32Value)1U };
// Cell Format ID = 0 (The Default Cell Format)
DocumentFormat.OpenXml.Spreadsheet.CellFormat cellFormat0 = new DocumentFormat.OpenXml.Spreadsheet.CellFormat()
{
NumberFormatId = (DocumentFormat.OpenXml.UInt32Value)0U,
FontId = (DocumentFormat.OpenXml.UInt32Value)0U,
FillId = (DocumentFormat.OpenXml.UInt32Value)0U,
BorderId = (DocumentFormat.OpenXml.UInt32Value)0U,
FormatId = (DocumentFormat.OpenXml.UInt32Value)0U
};
cellFormats0.Append(cellFormat0);
// Cell Format ID = 1
DocumentFormat.OpenXml.Spreadsheet.CellFormat cellFormat1 = new DocumentFormat.OpenXml.Spreadsheet.CellFormat()
{
NumberFormatId = (DocumentFormat.OpenXml.UInt32Value)1U,
FontId = (DocumentFormat.OpenXml.UInt32Value)1U,
FillId = (DocumentFormat.OpenXml.UInt32Value)2U,
BorderId = (DocumentFormat.OpenXml.UInt32Value)1U,
FormatId = (DocumentFormat.OpenXml.UInt32Value)1U,
ApplyBorder = true,
ApplyFont = true,
ApplyFill = true,
};
cellFormats0.Append(cellFormat1);
DocumentFormat.OpenXml.Spreadsheet.CellStyles cellStyles = new DocumentFormat.OpenXml.Spreadsheet.CellStyles() { Count = (DocumentFormat.OpenXml.UInt32Value)1U };
DocumentFormat.OpenXml.Spreadsheet.CellStyle cellStyle = new DocumentFormat.OpenXml.Spreadsheet.CellStyle()
{
Name = "Normal",
FormatId = (DocumentFormat.OpenXml.UInt32Value)0U,
BuiltinId = (DocumentFormat.OpenXml.UInt32Value)1U
};
cellStyles.Append(cellStyle);
#endregion
#region Stylesheet
stylesheet1.Append(fonts);
stylesheet1.Append(fills);
stylesheet1.Append(borders);
stylesheet1.Append(cellFormats0);
stylesheet1.Append(cellStyles);
#endregion
return stylesheet1;
}

Using HighCharts with Lambda Expression

I am experiencing trouble getting what I am looking for, in respect to setting series for highcharts. I want to use my table in my database to post the number for the y-axis.
So in my table I have the properties, ID, TeamName, TotalWins.
I only have 2 records
ID = 1, TeamName = Boston Red Sox, TotalWins? = 0 nullable because the MLB season hasn't started yet
ID = 2, TeamName = Baltimore Orioles, TotalWins? = 0
Here is my ActionResult for my Chart:
public ActionResult Chart()
{
Highcharts chart = new Highcharts("chart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Pie })
.SetTitle(new Title { Text = "Who Has more Wins?" })
.SetSubtitle(new Subtitle { Text = "Source: Sportscenter" })
.SetXAxis(new XAxis
{
Categories = new[] { "Boston Red Sox", "Baltimore Orioles" },
Title = new XAxisTitle { Text = "Teams" }
})
.SetYAxis(new YAxis
{
Min = 0,
Title = new YAxisTitle
{
Text = "Wins (Game)",
Align = AxisTitleAligns.High
}
})
.SetTooltip(new Tooltip { Formatter = "function() { return ''+ this.series.name +': '+ this.y +' millions'; }" })
.SetPlotOptions(new PlotOptions
{
Bar = new PlotOptionsBar
{
DataLabels = new PlotOptionsBarDataLabels { Enabled = true }
}
})
.SetLegend(new Legend
{
Layout = Layouts.Horizontal,
Align = HorizontalAligns.Right,
VerticalAlign = VerticalAligns.Top,
X = -100,
Y = 100,
Floating = true,
BorderWidth = 1,
BackgroundColor = new BackColorOrGradient(ColorTranslator.FromHtml("#FFFFFF")),
Shadow = true
})
.SetCredits(new Credits { Enabled = false })
.SetSeries(new[]
{
new Series { Data = new Data(new object[] { db.Teams.Where(x => x.TeamName == "Boston Red Sox").Count(x => x.TotalWins) /*where the issue is */ }) },
});
return View(chart);
}
cannot implicitly convert type 'int?' to 'bool'
How do I set this lambda expression so that it retrieves the total wins for the boston red sox and then again for the baltimore orioles?
The problem is here:
.Count(x => x.TotalWins)
Count either takes no arguments (in which case it returns the total count of the results of the preceding query), or a lambda that returns a boolean expression, in which case it returns a count of items that meet the criteria.
Do you mean .Sum(x => x.TotalWins)?

Change color of bars depending on value in Highchart bar-chart with MVC3

I am using Dotnet Highchart with MVC3
I am currently working with a diagram that looks like this:
I am trying to modify my code so I can change color on the bars depending on what number they have. I also wonder how I can remove the button "Snittbetyg" as you see can on the image.
This is my code:
public ActionResult OfficeStatistic()
{
{
Highcharts chart1 = new Highcharts("chart1")
.SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = new Data(new object[] { 1, 8, 9, 6 }), Name = "Snittbetyg" })
.SetTitle(new Title { Text = "Örebro Statistik" })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column });
return View(chart1);
}
}
Any kind of help is appreciated.
Thanks in advance!
I haven't used Highchart but you can download examples from their codeplex page. It looks like both of your requirements can be achieved easily.
Remove the "Snittbetyg" button
Disable the legend:
.SetLegend(new Legend { Enabled = false });
Add Colours
For the series data use points instead of just the numbers:
Data data = new Data(new[]
{
new Point { Y = 1, Color = System.Drawing.Color.Red },
new Point { Y = 8, Color = System.Drawing.Color.Blue },
new Point { Y = 9, Color = System.Drawing.Color.Green },
new Point { Y = 6, Color = System.Drawing.Color.Black }
});
Highcharts chart1 = new Highcharts("chart1")
.SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = data, Name = "Snittbetyg" })
.SetTitle(new Title { Text = "Örebro Statistik" })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetLegend(new Legend { Enabled = false });
There doesn't seem to be a built in way to make highchart automatically colour the bar based on the y-value. I believe you would have to pick the colour yourself, e.g:
private System.Drawing.Color GetBarColour(int value)
{
if (value < 5) return System.Drawing.Color.Red;
if (value > 7) return System.Drawing.Color.Green;
return System.Drawing.Color.Orange;
}
public ActionResult OfficeStatistic()
{
{
var dataItems = new[] {1, 8, 9, 6};
Data data = new Data(
dataItems.Select(y => new Point {Color = GetBarColour(y), Y = y}).ToArray()
);
Highcharts chart1 = new Highcharts("chart1")
.SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = data, Name = "Snittbetyg" })
.SetTitle(new Title { Text = "Örebro Statistik" })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetLegend(new Legend { Enabled = false });
First, define a Tuple list first item is for color and second item point value
List<Tuple<string, Object>> dataItems = new List<Tuple<string, Object>>();
i am passing value with swtich it is not neccessary
SqlDataReader reader = myComm.ExecuteReader();
if (reader.HasRows)
{
string colorName ="";
while (reader.Read())
{
switch ((string)reader.GetValue(1))
{
case "Total Employee(s)":
colorName = "Blue";
break;
case "Present":
colorName = "Green";
break;
case "Late":
case"Absent":
case "During Less":
case "Early Going":
colorName = "Red";
break;
case "Leave":
colorName = "Orange";
break;
default:
colorName = "Gray";
break;
}
dataItems.Add(new Tuple<string, Object>(colorName, reader.GetValue(2)));
}
Now, Finally add Data into series object
new Series{
Name = "Employees",
Data = new Data(
dataItems.Select(y => new Point {
Color = System.Drawing.Color.FromName(y.Item1),
Y = (int)y.Item2 }).ToArray()
)
}

Categories

Resources