坑:如果是SideBySideStackedBarSeriesView的话, 看看有 多少个 Group 需要 注意一下
private void chartControl1_CustomPaint(object sender, DevExpress.XtraCharts.CustomPaintEventArgs e) {
var chart = ( ChartControl ) sender;
var diagram = ( XYDiagram ) chart.Diagram;
int barWidth = GetBarWidth(chart);
int barHeight = 0;
int barLocation = 0;
var pt1 = diagram.DiagramToPoint(1, 1);
var pt10 = diagram.DiagramToPoint(1, 0);
var pt2 = diagram.DiagramToPoint(2, 2);
var distance = pt2.Point.X - pt1.Point.X;
this.barWidth.Text = (( SideBySideStackedBarSeriesView ) chart.Series [0].View).BarWidth.ToString();
this.barWidth2.Text = barWidth.ToString();
this.spacing.Text = distance.ToString(); ;
int height = pt10.Point.Y - pt1.Point.Y;
e.Graphics.DrawRectangle(new Pen(Color.Black), new Rectangle(
new Point(pt1.Point.X - (barWidth / 2), pt1.Point.Y)
, new Size((pt2.Point.X - pt1.Point.X + barWidth), height)));
}
public int GetXDistinceInPixel(XYDiagram diagram, string x1, int y1, string x2, int y2) {
var pt1 = diagram.DiagramToPoint(1, 1);
var pt2 = diagram.DiagramToPoint(2, 2);
return pt2.Point.X - pt1.Point.X;
}
public int GetYDistinceInPixel(XYDiagram diagram, string x1, int y1, string x2, int y2) {
var pt1 = diagram.DiagramToPoint(1, 1);
var pt2 = diagram.DiagramToPoint(2, 2);
return pt2.Point.Y - pt1.Point.Y;
}
public int GetBarWidth(ChartControl chart) {
int barWidth = 0;
if (chart.Series.Count > 0 && chart.Series [0].Points.Count > 1) {
ControlCoordinates cc1 = (chart.Diagram as XYDiagram2D).DiagramToPoint(chart.Series ["Series 3"].Points [0].Argument, 0);
ControlCoordinates cc2 = (chart.Diagram as XYDiagram2D).DiagramToPoint(chart.Series ["Series 3"].Points [1].Argument, 0);
barWidth = Convert.ToInt32(
Math.Round((cc2.Point.X - cc1.Point.X) * (( SideBySideStackedBarSeriesView ) chart.Series [0].View).BarWidth, 0)
);
}
return barWidth;
}