我正在使用System.Windows.Forms.DataVisualization.Charting.Chart
在我的应用程序中绘制图表并将其作为图像导出,以便在其他地方使用。
我能够计算并显示直方图的结果。我面临的问题是我还需要显示它在列上方发生的百分比。
我考虑使用堆叠列来实现此效果,方法是让百分比显示为原始列上方的值。 但是,它会出现在侧面,所以我不确定我做错了什么。
以下是我当前实现的代码片段:
RecomputeHistogramValues();
DateTime startRange = profile.GetStartDate();
DateTime endRange = profile.GetEndDate();
Int32 length = m_lHistogramBins.Count;
Int32 width = (Int32)(7.3 * 300); // 300 dpi.
Int32 height = (Int32)(3.5 * 300); // 300 dpi
Chart chart = new Chart
{
Size = new Size(width, height)
};
ChartArea chartArea = new ChartArea();
chartArea.AxisX.MajorGrid.Enabled = false;
chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None;
chartArea.AxisX.LabelStyle.Font = new Font("Calibri", 36);
chartArea.AxisX.LabelStyle.Angle = 0;
chartArea.AxisY.LabelAutoFitStyle = LabelAutoFitStyles.None;
chartArea.AxisY.LabelStyle.Font = new Font("Calibri", 36);
chartArea.AxisX.TitleFont = new Font("Calibri", 36);
chartArea.AxisX.Title = "Load";
chartArea.AxisX.TextOrientation = TextOrientation.Horizontal;
chartArea.AxisY.TitleFont = new Font("Calibri", 36);
chartArea.AxisY.Title = "Occurrences";
chartArea.AxisY.TextOrientation = TextOrientation.Rotated270;
chartArea.AxisX2.Enabled = AxisEnabled.False;
chartArea.AxisY2.Enabled = AxisEnabled.False;
chart.ChartAreas.Add(chartArea);
SeriesCollection seriesCollection = chart.Series;
// Generation of series
Series series = new Series
{
ChartType = SeriesChartType.StackedColumn,
XValueType = ChartValueType.String,
YValueType = ChartValueType.Int32,
Color = s_iSeries1Argb
};
Series percentages = new Series
{
ChartType = SeriesChartType.StackedColumn,
XValueType = ChartValueType.String,
YValueType = ChartValueType.Double,
Color = Color.Transparent
};
for (Int32 i = 0; i < length; ++i)
{
if (i < length - 1)
{
String label = m_lHistogramBins[i].ToString();
Double datapoint = Convert.ToDouble(m_hFrequency[m_lHistogramBins[i]]);
series.Points.AddXY(label, datapoint);
percentages.Points.AddXY(label, datapoint / m_iTotalFrequency);
}
else
{
String label = String.Format(">{0}", m_lHistogramBins[i - 1]);
Double datapoint = Convert.ToDouble(m_hFrequency[m_lHistogramBins[i]]);
series.Points.AddXY(label, datapoint);
percentages.Points.AddXY(label, datapoint / m_iTotalFrequency);
}
}
percentages.IsValueShownAsLabel = true;
percentages.LabelFormat = "P1";
percentages.YAxisType = AxisType.Secondary;
percentages.Enabled = true;
percentages.Font = new Font("Calibri", 36);
seriesCollection.Add(series);
seriesCollection.Add(percentages);
Title title = new Title
{
Text = String.Format("Histogram: {0} - {1}", startRange.ToShortDateString(), endRange.ToShortDateString()),
Font = new Font("Calibri", 48, FontStyle.Bold)
};
chart.Titles.Add(title);
chart.Invalidate();
chart.SaveImage(m_sChartFilename, ChartImageFormat.Png);
非常感谢任何建议。