绘制重叠列或条形图

时间:2014-03-27 14:24:21

标签: c# charts mschart

我需要使用列或条形图将4个系列数据绘制到MSChart。我可以绘制这4个系列,以便数据重叠而不是堆叠。

我刚发现Office Excel有一个ChartGroup.Overlap属性。

我如何在MSChart中完成?如果没有,图表控件可以做什么?任何信息将不胜感激。

2 个答案:

答案 0 :(得分:1)

不确定这是否是理想的解决方案,但如果您在图表中创建两个ChartArea,然后只绘制一个在另一个上面,则可以重叠系列。这需要大量摆弄位置,大小,轴等,以使它们排队,所以需要一些努力,但产生以下

enter image description here

Chart _chart = new Chart();
TabPage2.Controls.Add(_chart);

_chart.Location = new Point(469, 37);
_chart.Name = "chart1";
_chart.Size = new Size(448, 260);


DataTable dt1 = new DataTable();
dt1.Columns.Add("XVals", typeof(string));
dt1.Columns.Add("YVals1", typeof(int));
dt1.Columns.Add("YVals2", typeof(int));

foreach (string c in "ABCDEF".ToCharArray()) {
    dt1.Rows.Add(c, Convert.ToInt32(Math.Ceiling(VBMath.Rnd() * 20)), Convert.ToInt32(Math.Ceiling(VBMath.Rnd() * 20)));
}

ChartArea firstArea = _chart.ChartAreas.Add("First Area");
Series seriesFirst = _chart.Series.Add("First Series");
seriesFirst.ChartType = SeriesChartType.Column;

ChartArea secondArea = _chart.ChartAreas.Add("Second Area");
secondArea.BackColor = Color.Transparent;
secondArea.AlignmentOrientation = AreaAlignmentOrientations.All;
secondArea.AlignmentStyle = AreaAlignmentStyles.All;
secondArea.AlignWithChartArea = firstArea.Name;
secondArea.AxisY.LabelStyle.Enabled = false;
secondArea.AxisX.LabelStyle.Enabled = false;

Series seriesSecond = _chart.Series.Add("Second Series");
seriesSecond.ChartType = SeriesChartType.Column;
seriesSecond.ChartArea = secondArea.Name;

_chart.DataSource = dt1;
seriesFirst.Points.DataBind(dt1.DefaultView, "XVals", "YVals1", null);
seriesSecond.Points.DataBind(dt1.DefaultView, "XVals", "YVals2", null);

//Aligning the Y axis of the two chart areas
//I am assuming here the x values for both series are similar and dont need to be altered
//If using bar chart then x axis would be modifed not the y axis
secondArea.AxisY = firstArea.AxisY;

// *** Set locational values here for your first chart area***
int heightAboveChartArea = 20;
int heightBelowChartArea = 20;
int axisLabelHeight = 40;
int widthLeftOfChartArea = 20;
int widthRightOfChartArea = 20;
int heightPerBar = 20;
int numberOfPoints = _chart.Series(0).Points.Count;

// *** The following code should not normally be modified ***
_chart.ChartAreas(0).Position.X = widthLeftOfChartArea / _chart.Width * 100;
_chart.ChartAreas(0).Position.Width = 100 - (widthRightOfChartArea / _chart.Width * 100) - _chart.ChartAreas(0).Position.X;
_chart.ChartAreas(0).Position.Y = (heightAboveChartArea / _chart.Height * 100);
_chart.ChartAreas(0).Position.Height = 100 - (heightBelowChartArea / _chart.Height * 100) - _chart.ChartAreas(0).Position.Y;

答案 1 :(得分:0)

C#图表具有每个系列的属性。 “列”图表类型具有属性,特别是对于该重叠问题

buildscript {
    ...
}

allprojects {
    repositories {
        google()
        //use maven repo
        mavenCentral()
        maven {
            url 'https://jitpack.io'
        }

        jcenter()
    }
}