Not sure, I thought I had understood correctly. That the goal is to line up the charts (?). The principle is the same in this case. To modify chart settings to obtain the required result.
ie. Very unrefined code to achieve these charts is included here:
Code: Select all
Line line1;
Line line2;
Line line3;
public Form1()
{
InitializeComponent();
line1 = new Line(tChart1.Chart);
line2 = new Line(tChart1.Chart);
line3 = new Line(tChart2.Chart);
var product1 = "P1";
var product2 = "P2";
InitializeChart(line1, product1, Color.Red);
InitializeChart(line2, product2, Color.Blue);
InitializeChart2(line3,Color.Red);
line3.Add(0, 10, "P1");
line3.Add(1, 18, "P2");
line3.Marks.Visible = true;
line3.Marks.Style = MarksStyles.LabelValue;
tChart1.Legend.Visible = false;
tChart2.Legend.Visible = false;
tChart1.Axes.Bottom.SetMinMax(-1, 2);
tChart1.Axes.Bottom.Grid.DrawEvery = 1;
tChart2.Axes.Bottom.SetMinMax(-1, 2);
tChart2.Axes.Bottom.Grid.DrawEvery = 1;
tChart2.Axes.Left.SetMinMax(0, 20);
tChart1.Axes.Left.Labels.Size.Width = 10;
tChart1.Axes.Left.Labels.Size.Width = 10;
tChart1.Axes.Bottom.Labels.Items.Add(0, product1);
tChart1.Axes.Bottom.Labels.Items.Add(1, product2);
tChart2.Axes.Bottom.Labels.Items.Add(0, "");
tChart2.Axes.Bottom.Labels.Items.Add(1, "");
tChart2.Axes.Bottom.Labels.Visible = false;
tChart1.Axes.Bottom.Grid.Visible = true;
tChart2.Axes.Bottom.Grid.Visible = true;
}
public class ProductInfo
{
public string Product { get; set; }
public int Y { get; set; }
public int X { get; set; }
}
List<ProductInfo> list = new List<ProductInfo>()
{
new ProductInfo() { X = 0, Product = "P1", Y = 10 },
new ProductInfo() { X = 0, Product = "P1", Y = 15 },
new ProductInfo() { X = 0, Product = "P1", Y = 6 },
new ProductInfo() { X = 1, Product = "P2", Y = 22 },
new ProductInfo() { X = 1, Product = "P2", Y = 18 }
};
private void InitializeChart2(Line line, Color color)
{
line.LinePen.Width = 3;
line.Color = color;
line.Pointer.Visible = true;
line.Pointer.Style = PointerStyles.Circle;
}
private void InitializeChart(Line line, string select, Color color)
{
line.LinePen.Width = 3;
line.Color = color;
line.Pointer.Visible = true;
line.Pointer.Style = PointerStyles.Circle;
line.YValues.DataMember = "Y";
line.XValues.DataMember = "X";
line.LabelMember = "Product";
line.Title = select;
line.Add(list.Where(x => x.Product == select).ToList() as IList);
}
private void tChart2_GetAxisLabel(object sender, GetAxisLabelEventArgs e)
{
if (((Axis)sender).Horizontal)
e.LabelText = "";
}
Marc