Hi
I just cant figure this out.
I just want a pie chart, where the legend displays witch type the value represents.
Im generating this image:
The pie chart in the bottom is fine but the legend dosn't show my "type" but the value.
Beside the legend question i have some other questions i just cant find the answer on:
How can i remove the stroke on the chart?
How can i change the text displayed on the chartmodel ex. instead of 9,4 i want 9.4 Ton
I use a custom class for the modelbinding:
ChartItem.cs:
-------------------------
public class ChartItem
{
private double _value;
private string _type;
private string _units;
public double Value
{
get { return _value; }
set { _value = value; }
}
public string Type
{
get { return _type; }
set { _type = value; }
}
public string Units
{
get { return _units; }
set { _units = value; }
}
}
-------------------------
And this the method where i'm generating the chart
-------------------------
public Image CreateChartImage(int width, int height, DataTable dt)
{
try
{
//Generer custom font
FontFamily fontFamily = new FontFamily("Geogrotesque Semibold");
Font font = new Font(
fontFamily,
12,
GraphicsUnit.Point);
ChartSeries chart = new ChartSeries("ProductionsChart")
{
Type = ChartSeriesType.Pie,
SmartLabels = true
};
var chartPointList = new List<ChartItem>();
foreach (DataRow row in dt.Rows)
{
chartPointList.Add(new ChartItem
{
Type = (string)row["Type"],
Value = (double)row["Value"],
Units = (string)row["Enhed"]
});
}
//DEBUG CODE Kul
chartPointList.Add(new ChartItem
{
Type = "Kul",
Units = "Ton",
Value = 3.75d
});
ChartDataBindModel dataSeriesModel = new ChartDataBindModel(chart);
dataSeriesModel.YNames = new string[] { "Value" };
dataSeriesModel.DataSource = chartPointList;
chart.SeriesModel = dataSeriesModel;
ChartDataBindAxisLabelModel dataLabelsModel = new ChartDataBindAxisLabelModel(chartPointList);
dataLabelsModel.LabelName = "Type";
ChartControl chartControl = new ChartControl(false);
chartControl.Series.Add(chart);
chartControl.PrimaryXAxis.LabelsImpl = dataLabelsModel;
//style
chartControl.Series[0].Style.DisplayText = true;
chartControl.Series[0].Style.Border.Color = Color.White;
chartControl.Series[0].Style.Border.Width = 2f;
chartControl.Series[0].Style.TextOrientation = ChartTextOrientation.RegionCenter;
chartControl.ChartArea.BorderStyle = BorderStyle.None;
chartControl.Model.ColorModel.CustomColors = new[] { Color.FromArgb(79, 129, 189), Color.FromArgb(71, 192, 53), Color.FromArgb(128, 100, 168) };
chartControl.Model.ColorModel.Palette = ChartColorPalette.Custom;
chartControl.ShowLegend = true;
chartControl.LegendPosition = ChartDock.Floating;
var savePath = @"c:\Temp\chart.png";
chartControl.Skins = Skins.Metro;
chartControl.ShowLegend = true;
chartControl.Width = 300;
chartControl.Height = 300;
chartControl.Font = font;
chartControl.ForeColor = Color.White;
chartControl.BackColor = Color.Transparent;
chartControl.SaveImage(savePath);
return new Bitmap(savePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return null;
}
-------------------------