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;
}
Related
The full error is : 'Source array was not long enough. Check the source index, length, and the array's lower bounds. (Parameter 'sourceArray')'
(IDataView trainingDataView, IDataView testDataView) = LoadData(mlContext); // Load
IDataView transformedNewData = dataPrepPipeline.Transform(trainingDataView);
var est = mlContext.Transforms.Categorical.OneHotEncoding("usernameEncoded", "username").Append(mlContext.Transforms.Categorical.OneHotEncoding("tagEncoded", "tag"));
var fitted = est.Fit(transformedNewData);
var data = fitted.Transform(transformedNewData);
var testEst = mlContext.Transforms.Categorical.OneHotEncoding("usernameEncoded", "username").Append(mlContext.Transforms.Categorical.OneHotEncoding("tagEncoded", "tag"));
var testFitted = testEst.Fit(testDataView);
var testData = testFitted.Transform(testDataView);
var options = new FieldAwareFactorizationMachineTrainer.Options
{
FeatureColumnName = "usernameEncoded",
ExtraFeatureColumns = new string[] { "tagEncoded" },
LabelColumnName = "Label",
LambdaLatent = 0.01f,
LambdaLinear = 0.001f,
LatentDimension = 16,
NumberOfIterations = 50,
LearningRate = 0.5f,
Verbose = true
};
FieldAwareFactorizationMachinePredictionTransformer retrainedModel =
mlContext.BinaryClassification.Trainers.FieldAwareFactorizationMachine(options).Fit(data, testData, originalModelParameters); // Error is here
my intention is to display a gps track and the corresponding trackpoints with Mapsui (wpf) on a map. I tried the following code. The result is that the blue linestring is displayed (ok), the red track points (ok) but for any reason you see white track points which are very large and I do not want them to appear at the map and I do not know where the white dots are coming from. Any idea what I am doing wrong?
protected ILayer CreateLineStringLayer(String name, List<GeoWaypoint> geoWaypoints)
{
var lineString = new LineString();
List<Feature> featureList = new List<Feature>();
IStyle pointStyle = new SymbolStyle()
{
SymbolScale = 0.30,
Fill = new Brush(Mapsui.Styles.Color.FromString("Red"))
};
foreach (var wp in geoWaypoints)
{
var point = SphericalMercator.FromLonLat(wp.Longitude, wp.Latitude);
lineString.Vertices.Add(point);
var p2 = SphericalMercator.FromLonLat(wp.Longitude, wp.Latitude);
var pointFeature = new Feature();
pointFeature.Geometry = p2;
pointFeature.Styles.Add(pointStyle);
featureList.Add(pointFeature);
}
IStyle linestringStyle = new VectorStyle()
{
Fill = null,
Outline = null,
Line = { Color = Mapsui.Styles.Color.FromString("Blue"), Width = 4 }
};
Feature lineStringFeature = new Feature()
{
Geometry = lineString
};
lineStringFeature.Styles.Add(linestringStyle);
featureList.Add(lineStringFeature);
MemoryProvider memoryProvider = new MemoryProvider(featureList);
return new MemoryLayer
{
DataSource = memoryProvider,
Name = name
};
}
so for everyone who is interest in the answer
return new MemoryLayer
{
DataSource = memoryProvider,
Name = name ,
Style = null
};
You need to set the value for Style to null for the Memorylayer
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
}
}
}
}
}
};
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:
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()
)
}