How to chart using Web.UI.DataVisualization.Charting? - c#

using System.Web.UI.DataVisualization.Charting.Chart
I have data in 2 formats:
A.
B.
The data in format A is what I am using to build a chart. But my chart ends up looking like:
I cannot figure out how come no lines are drawn. Here is the code I am using:
var dataTable = GetDataTable();
var xAxisTitle = dataTable.Columns[1].ExtendedProperties["Type"].ToString();
var yAxisTitle = dataTable.Columns[0].ExtendedProperties["Type"].ToString();
chart = new Chart() {
AntiAliasing = AntiAliasingStyles.All,
TextAntiAliasingQuality = TextAntiAliasingQuality.High,
Width = 1200,
Height = 800,
Enabled = true,
ForeColor = Color.SaddleBrown
};
var chartArea = new ChartArea() {
BackColor = Color.White,
AxisY = new Axis() {
Enabled = AxisEnabled.True,
Title = yAxisTitle,
LineColor = Color.DarkBlue,
MajorTickMark = new TickMark() {
Enabled = true,
LineColor = Color.DarkGreen,
Interval = .1d
},
MinorTickMark = new TickMark() {
Enabled = true,
LineColor = Color.Green,
Interval = .1d
},
LabelStyle = new LabelStyle() {
Enabled = true,
ForeColor = Color.Red,
IsEndLabelVisible = true,
Font = new Font("Calibri", 4, FontStyle.Regular)
},
MajorGrid = new Grid() {
Enabled = true,
LineColor = Color.LightGray,
LineWidth = 1
},
},
AxisX = new Axis() {
Enabled = AxisEnabled.True,
Title = xAxisTitle,
LineColor = Color.DarkBlue,
MajorTickMark = new TickMark() {
Enabled = true,
LineColor = Color.Red,
Interval = .1d
},
MinorTickMark = new TickMark() {
Enabled = true,
LineColor = Color.DarkGreen,
Interval = .1d
},
LabelStyle = new LabelStyle() {
Enabled = true,
ForeColor = Color.Blue,
IsEndLabelVisible = true,
Font = new Font("Calibri", 4, FontStyle.Regular)
},
MajorGrid = new Grid() {
Enabled = true,
LineColor = Color.DarkGray,
LineWidth = 1
},
},
};
chartArea.AxisX.Enabled = AxisEnabled.True;
chartArea.AxisY.Enabled = AxisEnabled.True;
chart.ChartAreas.Add(chartArea);
var lineHeaders = dataTable.Rows
.OfType<DataRow>()
.Select(r => r[0].ToString())
.ToArray();
var i = 0;
for (int column = 0; column < lineHeaders.Length; column++) {
var header = lineHeaders[column];
var series = chart.Series[header] = new Series() {
Enabled = true,
Name = header,
Font = new Font("Lucida Sans Unicode", 6f),
Color = legendColors[header],
ChartType = SeriesChartType.Line,
XValueType = ChartValueType.DateTime,
YValueType = ChartValueType.Double,
};
var colData = dataTable.Rows[column]
.ItemArray
.Skip(1)
.Select(d => (double)d)
.ToArray();
DataPoint p = new DataPoint() {
AxisLabel = header,
XValue = i++,
YValues = colData,
};
series.Points.Add(p);
}
I've also set the data table as the data source on the Chart but there are no data points plotted.
Am I even using the correct data table ...should I be using format B instead?

I proceeded with the table format option B:
Other codes changes were minimal but re-posting the entire snippet below. The primary fix are these two lines at the end of the method:
chart.DataSource = DataTable;
chart.DataBind();
internal void BuildChart(DataTable DataTable) {
var xAxisTitle = DataTable.Columns[1].ExtendedProperties["Type"].ToString();
var yAxisTitle = DataTable.Columns[0].ExtendedProperties["Type"].ToString();
chart = new Chart() {
AntiAliasing = AntiAliasingStyles.All,
TextAntiAliasingQuality = TextAntiAliasingQuality.High,
Width = 1800,
Height = 500,
Enabled = true,
ForeColor = Color.SaddleBrown
};
#region build chart area
var chartArea = new ChartArea() {
BackColor = Color.White,
Name = DataTable.TableName,
AxisY = new Axis() {
Enabled = AxisEnabled.True,
Title = yAxisTitle,
LineColor = Color.DarkBlue,
MajorTickMark = new TickMark() {
Enabled = true,
LineColor = Color.DarkGreen,
},
MinorTickMark = new TickMark() {
Enabled = true,
LineColor = Color.Green,
},
LabelStyle = new LabelStyle() {
Enabled = true,
ForeColor = Color.Red,
IsEndLabelVisible = true,
Font = new Font("Calibri", 4, FontStyle.Regular)
},
MajorGrid = new Grid() {
Enabled = true,
LineColor = Color.LightGray,
LineWidth = 1
},
},
AxisX = new Axis() {
Enabled = AxisEnabled.True,
Title = xAxisTitle,
LineColor = Color.DarkBlue,
MajorTickMark = new TickMark() {
Enabled = true,
LineColor = Color.Red,
},
MinorTickMark = new TickMark() {
Enabled = true,
LineColor = Color.DarkGreen,
},
LabelStyle = new LabelStyle() {
Enabled = true,
ForeColor = Color.Blue,
IsEndLabelVisible = true,
Font = new Font("Calibri", 4, FontStyle.Regular)
},
MajorGrid = new Grid() {
Enabled = true,
LineColor = Color.DarkGray,
LineWidth = 1
},
},
};
chartArea.AxisX.Enabled = AxisEnabled.True;
chartArea.AxisY.Enabled = AxisEnabled.True;
#endregion
chart.ChartAreas.Add(chartArea);
var seriesHeaders = DataTable.Columns
.OfType<DataColumn>()
.Skip(1)
.Select(c => c.ColumnName)
.ToArray();
chart.Legends.Add(new Legend(DataTable.TableName) {
Enabled = true
});
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.MarkerColor = legendColors[header];
}
chart.DataSource = DataTable;
chart.DataBind();
}

Related

WPF Bar Chart how to add space between bars?

I am using Telerik (RadPieChart) with WPF. What I want to do is add a small space between the bars. I am not asking about the space between the series, as that is already available, but about a smaller space between the bars just as shown in the image examples below.
Here is what I have now:
And this is how I would like my Bar Chart to look like with a small space between them:
This is my source code:
private BarSeries CreateBarSeries(KeyValuePair<ChartSerie, List<ChartDataPoint>> chartSeries, ChartLegendSettings legendSettings, int colorPaletteIndex)
{
var isStackMode = chartSeries.Key.CombineMode == SeriesCombineMode.Stack;
var barSerie = new BarSeries()
{
VerticalAxis = CreateMultipleVerticalAxis(chartSeries, colorPaletteIndex, out var multipleVerticalAxis) ? multipleVerticalAxis : null,
LegendSettings = legendSettings,
StackGroupKey = chartSeries.Key.Group,
Opacity = 0.8,
ZIndex = 120,
CombineMode = string.IsNullOrEmpty(chartSeries.Key.Group)
? ChartSeriesCombineMode.Cluster
: (isStackMode ? ChartSeriesCombineMode.Stack : ChartSeriesCombineMode.Stack100),
// start animations
//PointAnimation = new ChartMoveAnimation()
//{
// MoveAnimationType = MoveAnimationType.Bottom,
// Duration = new TimeSpan(0, 0, 0, 0, 600),
// Delay = new TimeSpan(0, 0, 0, 0, 155),
// //Easing = new ElasticEase()
// //{
// // EasingMode = EasingMode.EaseOut,
// //},
//},
LabelDefinitions =
{
// set the clarion format for the labels
new ChartSeriesLabelDefinition()
{
Template = new DataTemplate()
{
VisualTree = GetSeriesFormat(chartSeries),
}
}
}
};
// this is the color of bar series
if (chartSeries.Key.ColorHex != null)
{
Style style = new Style(typeof(Border));
style.Setters.Add(new Setter(Border.BackgroundProperty, (SolidColorBrush)(new BrushConverter().ConvertFrom(chartSeries.Key.ColorHex))));
barSerie.DefaultVisualStyle = style;
}
foreach (ChartDataPoint serie in chartSeries.Value)
{
barSerie.DataPoints.Add(new CategoricalDataPoint()
{
Category = serie.XPoint.Label,
Value = (double?)serie.Value,
});
}
return barSerie;
}
The answer:
For some reason adding the BorderThickness to the Style as suggested in one of the answers did not do the trick, although BorderThicknes should be the solution. So I added a PointTemplate with a VisualTree and there I defined the BorderThickness. Now it is working perfectly.
private BarSeries CreateBarSeries(KeyValuePair<ChartSerie, List<ChartDataPoint>> chartSeries, ChartLegendSettings legendSettings, int colorPaletteIndex)
{
var seriesPredefinedColor = this.ChartBase.Palette.GlobalEntries[colorPaletteIndex].Fill;
FrameworkElementFactory borderFramework = new FrameworkElementFactory(typeof(Border));
borderFramework.SetValue(Border.BackgroundProperty, ColorService.BrushFromHex(chartSeries.Key.ColorHex) ?? seriesPredefinedColor);
borderFramework.SetValue(Border.OpacityProperty, 0.8D);
borderFramework.SetValue(Border.BorderThicknessProperty, new Thickness(2));
borderFramework.AddHandler(Border.MouseEnterEvent, new MouseEventHandler((sender, args) =>
{
var seriesBorder = (Border)sender;
//seriesBorder.BorderBrush = new SolidColorBrush(Colors.Black);
//seriesBorder.BorderThickness = new Thickness(1);
seriesBorder.Opacity = 1;
}));
borderFramework.AddHandler(Border.MouseLeaveEvent, new MouseEventHandler((sender, args) =>
{
var seriesBorder = (Border)sender;
//seriesBorder.BorderBrush = new SolidColorBrush(Colors.Black);
//seriesBorder.BorderThickness= new Thickness(1);
seriesBorder.Opacity = 0.8;
}));
var isStackMode = chartSeries.Key.CombineMode == SeriesCombineMode.Stack;
var barSerie = new BarSeries()
{
VerticalAxis = CreateMultipleVerticalAxis(chartSeries, colorPaletteIndex, out var multipleVerticalAxis) ? multipleVerticalAxis : null,
LegendSettings = legendSettings,
StackGroupKey = chartSeries.Key.Group,
ZIndex = 120,
IsHitTestVisible = true,
CombineMode = string.IsNullOrEmpty(chartSeries.Key.Group)
? ChartSeriesCombineMode.Cluster
: (isStackMode ? ChartSeriesCombineMode.Stack : ChartSeriesCombineMode.Stack100),
// start animations
//PointAnimation = new ChartMoveAnimation()
//{
// MoveAnimationType = MoveAnimationType.Bottom,
// Duration = new TimeSpan(0, 0, 0, 0, 600),
// Delay = new TimeSpan(0, 0, 0, 0, 155),
// //Easing = new ElasticEase()
// //{
// // EasingMode = EasingMode.EaseOut,
// //},
//},
LabelDefinitions =
{
// set the clarion format for the labels
new ChartSeriesLabelDefinition()
{
Template = new DataTemplate()
{
VisualTree = GetSeriesFormat(chartSeries),
}
}
},
PointTemplate = new DataTemplate()
{
VisualTree = borderFramework,
}
};
// this is the color of bar series
//if (chartSeries.Key.ColorHex != null)
//{
// Style style = new Style(typeof(Border));
// style.Setters.Add(new Setter(Border.BackgroundProperty, (SolidColorBrush)(new BrushConverter().ConvertFrom(chartSeries.Key.ColorHex))));
// barSerie.DefaultVisualStyle = style;
//}
foreach (ChartDataPoint serie in chartSeries.Value)
{
barSerie.DataPoints.Add(new CategoricalDataPoint()
{
Category = serie.XPoint.Label,
Value = (double?)serie.Value,
});
}
return barSerie;
}
Set the BorderThickness property of the DefaultVisualStyle of the BarSeries:
// this is the color of bar series
if (chartSeries.Key.ColorHex != null)
{
Style style = new Style(typeof(Border));
style.Setters.Add(new Setter(Border.BackgroundProperty, (SolidColorBrush)(new BrushConverter().ConvertFrom(chartSeries.Key.ColorHex))));
style.Setters.Add(new Setter(Border.BorderThicknessProperty, new Thickness(2.0)));
barSerie.DefaultVisualStyle = style;
}
have you look into this ? seem default is 0 mean bar take all the space
Scale.SpacingSlotCount Property
Determines the number of space slots that will be left around the DataPoints per category slot, measured relatively to the DataPoint
slot's width: Empty Space = SpacingSlotCount * DataPoint_SlotWidth
Namespace: Telerik.Reporting
Assembly: Telerik.Reporting (in Telerik.Reporting.dll) Version: 12.1.18.816 (12.1.18.816)

i need audio frequency response graph c#

i need logarithm x-axis based linechart for audio frequency magnitude response for c# winform , i have tested default chart and livechart but c'ant align x axis attached photo like . anybody know please help me.
sorry for my bad english
Live Chart Code :
cartesianChart1.AxisY.Add(new Axis
{
Title = "Gain",
MaxValue = 10,
MinValue = -15,
Separator = new Separator
{
Stroke = new SolidColorBrush(System.Windows.Media.Color.FromRgb(46, 35, 35))
}
});
cartesianChart1.AxisX.Add(new LogarithmicAxis
{
Title = "Freq",
LabelFormatter = (value) => (Math.Pow(10, value).ToString("N0")+"Hz"),
Base = 10,
Separator = new Separator
{
Step=0,
Stroke = new SolidColorBrush(System.Windows.Media.Color.FromRgb(35, 35, 35))
},
});
cartesianChart1.BackColor = System.Drawing.Color.FromArgb(0, 0, 0);
cartesianChart1.DisableAnimations = true;
cartesianChart1.Hoverable = false;
cartesianChart1.DataTooltip = null;
var Datapoint = new ChartValues<ObservablePoint>{
new ObservablePoint(1, 5),
new ObservablePoint(5, -4),
new ObservablePoint(10, 6),
new ObservablePoint(100, 4),
new ObservablePoint(150, 7),
new ObservablePoint(1000, 2),
new ObservablePoint(10000, 8),
new ObservablePoint(15000, 2),
new ObservablePoint(20000, -7),
};
cartesianChart1.Series = new SeriesCollection(Mappers.Xy<ObservablePoint>()
.X(point => Math.Log10(point.X))
.Y(point => point.Y))
{
new LineSeries
{
PointGeometry = null,
PointGeometrySize = 0,
Values = Datapoint,
}
};
Live Chart Display :
Default Chart Code :
var r = new Random();
float val;
var chart = chart1.ChartAreas[0];
chart.AxisX.IntervalType = DateTimeIntervalType.Number;
chart.AxisX.LabelStyle.Format = "";
chart.AxisY.LabelStyle.Format = "";
chart.AxisY.LabelStyle.IsEndLabelVisible = true;
chart.AxisX.Minimum = 0;
chart.AxisX.Maximum = 20000;
chart.AxisY.Minimum = -15;
chart.AxisY.Maximum = 10;
chart.AxisX.Interval = 500;
chart.AxisY.Interval = 3;
chart.BackColor = Color.Black;
chart1.Series.Add("Sakthi");
chart1.Series["Sakthi"].ChartType = SeriesChartType.Spline;
chart1.Series["Sakthi"].Color = Color.Yellow;
chart1.Series["Sakthi"].BorderWidth = 3;
chart1.Series["Sakthi"].BorderColor = Color.Black;
chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.FromArgb(35, 35, 35);
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.FromArgb(35, 35, 35);
chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
chart1.Legends[0].Enabled = false;
chart.AxisY.StripLines.Add(new StripLine
{
BorderDashStyle = ChartDashStyle.Dot,
BorderColor = Color.White,
StripWidth = 0
});
chart1.Series.Clear();
chart1.Series.Add("Sakthi");
chart1.Series["Sakthi"].ChartType = SeriesChartType.Spline;
chart1.Series["Sakthi"].Color = Color.Yellow;
chart1.Series["Sakthi"].BorderWidth = 3;
chart1.Series["Sakthi"].BorderColor = Color.Black;
for (int i = 0; i < 10; i++)
{
val = r.Next(-15, 10);
chart1.Series["Sakthi"].Points.AddXY(i*2000, val);
}
Default Chart Display :
i want this this type x axis label :

How To Reduce Column Series Gap In DotNet.Highcharts

I am creating my chart using DotNet.Highchart Library, i have a problem in reducing gap between data series in column chart. This is my current Chart :
and this is my current column chart code:
DotNet.Highcharts.Highcharts AttritionByReasonBarChart = new DotNet.Highcharts.Highcharts("AttritionByReasonBarChart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column, Height = 400, Width = 860, Style = "margin: '0 auto'" })
.SetTitle(new Title { Text = "Attrition by Reason", Style = "font: 'normal 16px Verdana, sans-serif'" })
.SetCredits(new Credits { Enabled = false })
.SetXAxis(new XAxis
{
Categories = vEmployment,
Labels = new XAxisLabels { Rotation = 0 }
})
.SetYAxis(new YAxis
{
Title = new YAxisTitle
{
Text = "Employment Type",
Align = AxisTitleAligns.Middle
}
})
.SetPlotOptions(new PlotOptions
{
Bar = new PlotOptionsBar
{
DataLabels = new PlotOptionsBarDataLabels { Enabled = true }
}
})
.SetLegend(new Legend
{
Layout = Layouts.Vertical,
Align = HorizontalAligns.Right,
VerticalAlign = VerticalAligns.Middle,
Floating = true,
BorderWidth = 1,
BackgroundColor = new BackColorOrGradient(ColorTranslator.FromHtml("#FFFFFF")),
Shadow = true
})
.SetSeries(
new Series
{
Name = "Head Count",
Data = new Data(vTotal)
});
Is there any best way to set the gap between data series?
Thanks
Replace PlotOptionsBar with PlotOptionsColumn and add PointPadding to your code as below:
Highcharts AttritionByReasonBarChart = new Highcharts("AttritionByReasonBarChart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column, Height = 400, Width = 860, Style = "margin: '0 auto'" })
.SetTitle(new Title { Text = "Attrition by Reason", Style = "font: 'normal 16px Verdana, sans-serif'" })
.SetCredits(new Credits { Enabled = false })
.SetXAxis(new XAxis
{
Categories = new[] { "ABHFGFGETTRRD AGAGGSGSGS", "IEKHLTOTUYYT IIWWIIWEUEUEU", "KMVMVBBV DGDFEFJ", "KRJG JGJGJGT", "HAHAHSHSGD OTERETETE ET", "HAHAHA PRGDGDGDGDG DGT", "NRIRITITITITI WP", "DFC AHAHSSGGDF" },
Labels = new XAxisLabels { Rotation = 0 }
})
.SetYAxis(new YAxis
{
Title = new YAxisTitle
{
Text = "Employment Type",
Align = AxisTitleAligns.Middle
}
})
.SetPlotOptions(new PlotOptions
{
Column = new PlotOptionsColumn
{
PointPadding = -0.2,
DataLabels = new PlotOptionsColumnDataLabels { Enabled = false }
}
})
.SetLegend(new Legend
{
Layout = Layouts.Vertical,
Align = HorizontalAligns.Right,
VerticalAlign = VerticalAligns.Middle,
Floating = true,
BorderWidth = 1,
BackgroundColor = new BackColorOrGradient(ColorTranslator.FromHtml("#FFFFFF")),
Shadow = true
})
.SetSeries(
new Series
{
Name = "Head Count",
Data = new Data(new object[] { 30,10,5,20,5,10,25,15 })
});

How to set an interval for tickmarks?

I'm trying to have tickmarks at an interval of 50. MajorGrid works fine, but I can not find any way to get the tickmarks of the yAxis align to the gridlines of the majorgrid. Currently I'm using this:
chart.ChartAreas.Add(new ChartArea("statistic")
{
AxisX = ...
AxisY = new Axis
{
MajorGrid =
new Grid
{
Enabled = true,
LineColor = Color.Black,
LineDashStyle = ChartDashStyle.Solid,
Interval = 50,
IntervalOffset = 0
},
Title = yAxisDesc,
Minimum = yAxisRange.Item1,
Maximum = yAxisRange.Item2
}
}
to get this:
I tried modifying MajorTickMark to no avail.
What do I have to change?
Try this:
private void Form1_Load(object sender, EventArgs e)
{
int xmax = 100;
chart1.ChartAreas[0].AxisX.IsLogarithmic = true;
chart1.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
chart1.ChartAreas[0].AxisX.MinorGrid.Interval = 1;
chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 50;
chart1.ChartAreas[0].AxisY.MajorTickMark.Interval = 50;
chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 50;
for (int x = 1; x < xmax; x++)
chart1.Series[0].Points.AddXY(x, 5 * x);
}
Thanks to jstreet,
I found the following solution:
AxisY =
new Axis
{
MajorGrid =
new Grid
{
Enabled = true,
LineColor = Color.Black,
LineDashStyle = ChartDashStyle.Solid,
Interval = 50,
IntervalOffset = 0
},
Title = yAxisDesc,
Minimum = yAxisRange.Item1,
Maximum = yAxisRange.Item2,
LabelStyle = new LabelStyle{Interval = 50, Enabled=true,IntervalOffset = 0,IsEndLabelVisible = true},
MajorTickMark =
new TickMark
{
Enabled = true,
Interval = 50,
IntervalOffset = 0,
},
}
However, using only MajorTickMark or LabelStyle doesn't result in the desired chart. This is it now:

Radar chart selective label rotation in System.Web.UI.DataVisualization.Charting?

I am trying to reproduce a radar chart in ASP.NET MVC.
This is what I should have
This is what I actually have
So far, it works, the odd colors are just for development.
But the label rotation of the bottom 3 labels is quite bad, and I can't seem to find out how to rotate them properly. Anyone ?
Also, why is it setting markers in a 20 interval step, when I set 25 ?
And addtionally, just for fun, is it possible to rotate the y-axis thick markers by 22.5 degrees, as in the sample ?
Here my code:
using System.Drawing;
using System.Web.UI.DataVisualization.Charting;
// http://stackoverflow.com/questions/6047961/c-sharp-chart-rotate-labels
public FileResult RadarSample()
{
int pixelWidth = 1000;
int pixelHeight = 1000;
// Populate series data
//string[] xValues = { "France", "Canada", "Germany", "USA", "Italy", "Spain", "Russia", "Sweden", "Japan" };
string[] xValues = { "Offene Aussenpolitik", "Liberale Wirtschaftspolitik", "Restriktive Finanzpolitik", "Law & Order", "Restriktive Migrationspolitik", "Ausgebauter Umweltschutz", "Ausgebauter Sozialstaat", "Liberale Gesellschaft" };
double[] yValues = { 80, 90, 45, 75, 37.5, 40, 28, 54 };
//double[] yValues = { 65.62, 75.54, 60.45, 34.73, 85.42, 55.9, 63.6, 55.1, 77.2 };
//double[] yValues2 = { 76.45, 23.78, 86.45, 30.76, 23.79, 35.67, 89.56, 67.45, 38.98 };
var Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
Chart1.BackColor = System.Drawing.Color.HotPink;
var area = new System.Web.UI.DataVisualization.Charting.ChartArea("ca1");
area.Area3DStyle.Enable3D = false;
area.AxisX.Interval = 1;
area.BackColor = System.Drawing.Color.Red;
//area.AxisY.Interval = 5;
area.AxisY.MajorTickMark.Enabled = false;
area.AxisY.MajorGrid.LineColor = Color.Gray;
area.AxisY.MajorGrid.Interval = 25;
area.AxisY.MinorTickMark.Enabled = false;
area.AxisY.MinorGrid.Interval = 5;
area.AxisY.MinorGrid.LineColor = Color.Yellow;
Chart1.ChartAreas.Add(area);
var series1 = new System.Web.UI.DataVisualization.Charting.Series();
var series2 = new System.Web.UI.DataVisualization.Charting.Series();
series1.Name = "Series1";
series2.Name = "Series2";
//series1.Color = System.Drawing.Color.Yellow;
series1.Color = System.Drawing.Color.FromArgb(100, 0, 0, 255);
//series1.SmartLabelStyle.Enabled = true;
//series1.LabelAngle = 90;
//Legend legend = new Legend();
////legend.Name = "mylegend";
//legend.Title = "Hello world";
//legend.BackColor = Color.Transparent;
//legend.BackColor = Color.Tomato;
//Chart1.Legends.Add(legend);
// series1.Legend = "mylegend";
series1.LegendText = "A";
series2.LegendText = "B";
// series1.Label = "kickme";
// series2.Label = "bar";
//series1.ChartArea = "ca1";
series1.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Radar;
series2.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Radar;
series1.ChartArea = "ca1";
series2.ChartArea = "ca1";
Chart1.Series.Add(series1);
//Chart1.Series.Add(series2);
Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues);
//Chart1.Series["Series2"].Points.DataBindXY(xValues, yValues2);
string[] astrRadarStyleList = new string[] { "Area", "Line", "Marker" }; // Fill, Line, or point
string[] astrAreaDrawingStyleList = new string[] { "Circle", "Polygon" }; // Shape
string[] astrLabelStyleList = new string[] { "Circular", "Radial", "Horizontal" };
string strRadarStyle = astrRadarStyleList[0];
string strAreaDrawingStyle = astrAreaDrawingStyleList[0];
string strLabelStyle = astrLabelStyleList[0];
Chart1.Width = System.Web.UI.WebControls.Unit.Pixel(pixelWidth);
Chart1.Height = System.Web.UI.WebControls.Unit.Pixel(pixelHeight);
// Set radar chart style
Chart1.Series["Series1"]["RadarDrawingStyle"] = strRadarStyle; // RadarStyleList.SelectedItem.Text;
//Chart1.Series["Series2"]["RadarDrawingStyle"] = strRadarStyle; // RadarStyleList.SelectedItem.Text;
if (strRadarStyle == "Area")
{
Chart1.Series["Series1"].BorderColor = Color.FromArgb(100, 100, 100);
Chart1.Series["Series1"].BorderWidth = 1;
// Chart1.Series["Series2"].BorderColor = Color.FromArgb(100, 100, 100);
// Chart1.Series["Series2"].BorderWidth = 1;
}
else if (strRadarStyle == "Line")
{
Chart1.Series["Series1"].BorderColor = Color.Empty;
Chart1.Series["Series1"].BorderWidth = 2;
// Chart1.Series["Series2"].BorderColor = Color.Empty;
// Chart1.Series["Series2"].BorderWidth = 2;
}
else if (strRadarStyle == "Marker")
{
Chart1.Series["Series1"].BorderColor = Color.Empty;
// Chart1.Series["Series2"].BorderColor = Color.Empty;
}
// Set circular area drawing style
Chart1.Series["Series1"]["AreaDrawingStyle"] = strAreaDrawingStyle; // AreaDrawingStyleList.SelectedItem.Text;
//Chart1.Series["Series2"]["AreaDrawingStyle"] = strAreaDrawingStyle; // AreaDrawingStyleList.SelectedItem.Text;
// Set labels style
Chart1.Series["Series1"]["CircularLabelsStyle"] = strLabelStyle; // LabelStyleList.SelectedItem.Text;
//Chart1.Series["Series2"]["CircularLabelsStyle"] = strLabelStyle; //LabelStyleList.SelectedItem.Text;
return Chart2Image(Chart1);
}
public FileResult Chart2Image(System.Web.UI.DataVisualization.Charting.Chart chart)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
chart.SaveImage(ms, System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png);
ms.Seek(0, System.IO.SeekOrigin.Begin);
return File(ms.ToArray(), "image/png", "mychart.png");
} // End Using ms
}
Try this for text direction of labels:
Chart1.Series["Series1"]["CircularLabelsStyle"] = "Horizontal";

Categories

Resources