问题
When creating charts using Microsoft Charts, I need to get the exact position of the grid on the chart.
This page says -- as I understand it -- that this can be achieved with InnerPlotPosition. However, when using this property, I get the following rectangle:
This rectangle seems to scale with the chart size; when resizing the chart, the rectangle shrinks, but still keeps on the outside of the grid on all edges. I have tried both adding and multiplying the sizes with a constant, but no single constant works for all sizes.
So, just to clarify, what I want is the outer black rectangle on the grid. Is there any way to get this?
(I have been thinking of scanning pixels from the edges, and detecting the first black pixel in each direction, but that sounds like a grotty hack.)
回答1:
I finally found a solution that works:
ChartArea a = chart.ChartAreas[0];
double x1 = a.AxisX.ValueToPixelPosition(a.AxisX.Minimum);
double x2 = a.AxisX.ValueToPixelPosition(a.AxisX.Maximum);
double y1 = a.AxisY.ValueToPixelPosition(a.AxisY.Maximum);
double y2 = a.AxisY.ValueToPixelPosition(a.AxisY.Minimum);
来源:https://stackoverflow.com/questions/7283394/ms-charts-getting-the-real-innerplotposition