I have a requirement to create line chart graphs that involve data from several sources, and I need to identify the sources in the x-axis legend (see the attached image for an example)
How would I achieve this effect with your graphing tools?
Hi Richard,
The easiest way to accomplish this is probably to combine the data sources together and plot one line. An alternative approach could be to set up each source as a separate series, and plot them each at a different location.
Please try this out and let me know if you need any further explanation.
To clarify, I am populating the grid with a NumberSeries object.
How can I use a NumberSeries object to achieve this effect?
I think I may have misunderstood your requirement. From the image, it looks like the data from the different sources combines to form a single line.
You can plot each data source in its own Chart Layer in a composite chart, and then shift the layers horizontally so that it looks like the provided image. For an example of how to use Chart Layers, please refer to this sample: http://www.infragistics.com/samples/windows-forms/chart/composite-chart-layers
Please let me know if I am still misunderstanding.
Is there a way to directly set the entire x-axis label text?
I could use the TitleBottom for this, and determine the spacing for the "labels" within the TitleBottom text string, as in the image below. But there is too much space between the bottom of the chart and the TitleBottom string. Is there a way to eliminate that vertical space? I have tried to set "Axis.X.Labels.Visible = false;" but it doesn't help.
If you use a Composite Chart, you can eliminate the space between the bottom of the chart and the TitleBottom string. The composite chart settings allow you to control the sizing and position of all the chart elements. I recommend using the chart wizard rather than doing this in code. I've taken a screenshot of the relevant settings and attached it here.
Note that MeasureType is set to Percentage, Width is set to 100%, and Height is set to 90%
Please try this out and let me know whether it works for your needs.
Mike,
I have implemented the composite chart, and as you indicated, I have more control over the titles and sizing. However, I cannot get the lines to display in the chart. I followed a composite line chart sample (done in VS 2010), and that project works. I also added a chart to my project and used the code from that. But I can't get it working in my logic - see the image below.
As I mentioned, I need to do this in code because for this app requires a variable number of charts to be displayed depending on the data. My code is also pasted below.
Several other questions:
1. I set the height of the chart to 400, but the chart display is not respecting that value.
2. What properties should I use to set the margins for the top and bottom titles?
3. How do I set the LineAppearance for lines in a composite chart
int height = 600; int width = 400; for (int index = 0; index < list.Count; index++) { List<ViewLabTestData> listEntry = list[index]; string title = ""; if (listEntry[0].LabTest.Contains("Impedance")) { labTest = "Impedance"; title = labTest; } else if (listEntry[0].LabTest.Contains("Insertion Loss")) { labTest = "Insertion Loss"; title = string.Format("{0} @ {1} {2}", labTest, listEntry[0].MarkerFrequency.ToString("#0.####"), Enums.GetStringFromValue<Enums.FrequencyBandwidthUoM>(listEntry[0].MarkerValue_UoM)); } int left = (index % 2 == 0) ? 0 : width + _chartSpacing; int top = (index / 2) * (height + _chartSpacing); ChartLayerAppearance chartLayer = null; UltraChart chart = InfragisticsChartHelper.CreateCompositeChart(out chartLayer, title, 12, width, height, left, top); chart.TitleBottom.Text = "Orders"; pnlGraphs.Controls.Add(chart); NumericSeries borderTop = InfragisticsChartHelper.CreateCompositeLine(chart, chartLayer, LineDrawStyle.Solid, 1, System.Drawing.Color.White); NumericSeries borderBottom = InfragisticsChartHelper.CreateCompositeLine(chart, chartLayer, LineDrawStyle.Solid, 1, System.Drawing.Color.White); NumericSeries min = InfragisticsChartHelper.CreateCompositeLine(chart, chartLayer, LineDrawStyle.Solid, 3, System.Drawing.Color.Red); NumericSeries max = InfragisticsChartHelper.CreateCompositeLine(chart, chartLayer, LineDrawStyle.Solid, 3, System.Drawing.Color.Red); NumericSeries value = InfragisticsChartHelper.CreateCompositeLine(chart, chartLayer, LineDrawStyle.Solid, 2, System.Drawing.Color.Black); NumericSeries control = InfragisticsChartHelper.CreateCompositeLine(chart, chartLayer, LineDrawStyle.Solid, 1, System.Drawing.Color.Black); List<string> orderNoList = listEntry.Select(o => o.OrderNumber).Distinct().OrderByDescending(o => o).ToList(); while (orderNoList.Count > 4) orderNoList.RemoveAt(4); int count = 0; double average = 0; foreach (ViewLabTestData entry in listEntry) { if (orderNoList.Contains(entry.OrderNumber)) { min.Points.Add(new NumericDataPoint((double)entry.MarkerValue_Min, "", false)); max.Points.Add(new NumericDataPoint((double)entry.MarkerValue_Max, "", false)); borderTop.Points.Add(new NumericDataPoint((double)entry.MarkerValue_Max + 0.5, "", false)); borderBottom.Points.Add(new NumericDataPoint((double)entry.MarkerValue_Min - 0.5, "", false)); value.Points.Add(new NumericDataPoint((double)entry.MarkerValue_TestValue, "", false)); average += (double)entry.MarkerValue_TestValue; count++; } } if (count > 0) { average /= count; for (int avgPoint = 0; avgPoint < count; avgPoint++) control.Points.Add(new NumericDataPoint(average, "", false)); } } if (list.Count > 0) { pnlGraphs.Width = (list.Count <= 1) ? width : (width * 2) + _chartSpacing; pnlGraphs.Height = (((list.Count / 2) + 1) * height) + ((list.Count / 2) * _chartSpacing); this.Size = new Size(pnlGraphs.Width + (_chartSpacing * 3), pnlGraphs.Height + (_chartSpacing * 4)); this.Left = (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - this.Size.Width) / 2; this.Top = (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - this.Size.Height) / 2; } else { ScreenHelper.DisplayWarning("There was no data to be graphed"); this.Close(); } public static UltraChart CreateCompositeChart( out ChartLayerAppearance chartLayer, string title, float fontSize, int width = 400, int height = 300, int left = 0, int top = 0, bool xAxis = false) { UltraChart chart = null; chartLayer = null; try { chart = SetChartDefaults(ChartType.Composite, title); chart.TitleTop.Font = new Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Regular); chart.TitleBottom.Font = new Font(FontFamily.GenericMonospace, fontSize - 4, FontStyle.Regular); //chart.TitleBottom.Margins.Top = 0; chart.TitleLeft.Visible = false; chart.TitleRight.Visible = false; chart.Location = new Point(left, top); chart.Size = new Size(width, height); ChartArea chartArea = new ChartArea(); chartArea.Key = "chartArea"; chartArea.GridPE = new PaintElement(); chartArea.PE = new PaintElement(); chart.CompositeChart.ChartAreas.Add(chartArea); AxisItem axisY = new AxisItem(); axisY.OrientationType = AxisNumber.Y_Axis; axisY.DataType = AxisDataType.Numeric; axisY.Key = "axisY"; axisY.Labels.FontColor = System.Drawing.Color.Empty; axisY.Labels.HorizontalAlign = StringAlignment.Near; axisY.Labels.ItemFormatString = "<DATA_VALUE:#,###.##>"; axisY.Labels.Orientation = TextOrientation.Horizontal; axisY.Labels.SeriesLabels.FontColor = System.Drawing.Color.Empty; axisY.Labels.SeriesLabels.HorizontalAlign = StringAlignment.Center; axisY.Labels.SeriesLabels.Orientation = TextOrientation.Horizontal; axisY.Labels.SeriesLabels.VerticalAlign = StringAlignment.Center; axisY.Labels.SeriesLabels.Visible = true; axisY.Labels.VerticalAlign = StringAlignment.Center; axisY.Labels.Visible = true; axisY.MajorGridLines.AlphaLevel = ((byte)(255)); axisY.MajorGridLines.Color = System.Drawing.Color.Gainsboro; axisY.MajorGridLines.DrawStyle = LineDrawStyle.Dot; axisY.MajorGridLines.Visible = true; axisY.MinorGridLines.AlphaLevel = ((byte)(255)); axisY.MinorGridLines.Color = System.Drawing.Color.LightGray; axisY.MinorGridLines.DrawStyle = LineDrawStyle.Dot; axisY.MinorGridLines.Visible = false; axisY.SetLabelAxisType = SetLabelAxisType.GroupBySeries; chartArea.Axes.Add(axisY); AxisItem axisX = new AxisItem(); if (xAxis) { axisX.DataType = AxisDataType.String; axisX.Labels.ItemFormatString = "<ITEM_LABEL>"; axisX.OrientationType = AxisNumber.X_Axis; axisX.SetLabelAxisType = SetLabelAxisType.ContinuousData; chartArea.Axes.Add(axisY); } else chart.Axis.X.Labels.Visible = false; chartLayer = new ChartLayerAppearance(); chartLayer.ChartType = ChartType.LineChart; chartLayer.Key = "chartLayer"; chartLayer.ChartArea = chartArea; chartLayer.ChartAreaKey = "chartArea"; chartLayer.AxisY = axisY; chartLayer.AxisYKey = "axisY"; if (xAxis) { chartLayer.AxisX = axisX; chartLayer.AxisXKey = "axisX"; } chart.CompositeChart.ChartLayers.Add(chartLayer); } catch (Exception ex) { } return chart; } public static NumericSeries CreateCompositeLine( UltraChart chart, ChartLayerAppearance chartLayer, LineDrawStyle drawStyle, int thickness, System.Drawing.Color color) { NumericSeries series = null; try { series = new NumericSeries(); series.PEs.Add(new PaintElement(color)); chart.CompositeChart.Series.Add(series); chartLayer.Series.Add(series); LineAppearance line = new LineAppearance(); line.LineStyle.DrawStyle = drawStyle; line.Thickness = thickness; // How to add line appearance object? } catch (Exception ex) { } return series; } private static UltraChart SetChartDefaults(ChartType chartType, string title) { UltraChart chart = null; try { chart = new UltraChart(); chart.ChartType = chartType; chart.Name = title; chart.TitleTop.Text = title; chart.TitleTop.Orientation = TextOrientation.Horizontal; chart.TitleTop.HorizontalAlign = StringAlignment.Center; chart.TitleBottom.Orientation = TextOrientation.Horizontal; chart.TitleBottom.HorizontalAlign = StringAlignment.Center; PaintElement paintElement = new Infragistics.UltraChart.Resources.Appearance.PaintElement(); GradientEffect gradientEffect = new Infragistics.UltraChart.Resources.Appearance.GradientEffect(); chart.Axis.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(248)))), ((int)(((byte)(220))))); chart.Axis.X.Labels.Font = new Font("Verdana", 7F); chart.Axis.X.Labels.FontColor = System.Drawing.Color.DimGray; chart.Axis.X.Labels.HorizontalAlign = StringAlignment.Near; chart.Axis.X.Labels.ItemFormatString = "<ITEM_LABEL>"; chart.Axis.X.Labels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.X.Labels.Orientation = TextOrientation.VerticalLeftFacing; chart.Axis.X.Labels.SeriesLabels.Font = new Font("Verdana", 7F); chart.Axis.X.Labels.SeriesLabels.FontColor = System.Drawing.Color.DimGray; chart.Axis.X.Labels.SeriesLabels.FormatString = ""; chart.Axis.X.Labels.SeriesLabels.HorizontalAlign = StringAlignment.Near; chart.Axis.X.Labels.SeriesLabels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.X.Labels.SeriesLabels.Orientation = TextOrientation.VerticalLeftFacing; chart.Axis.X.Labels.SeriesLabels.VerticalAlign = StringAlignment.Center; chart.Axis.X.Labels.SeriesLabels.Visible = true; chart.Axis.X.Labels.VerticalAlign = StringAlignment.Center; chart.Axis.X.Labels.Visible = true; chart.Axis.X.LineThickness = 1; chart.Axis.X.MajorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.X.MajorGridLines.Color = System.Drawing.Color.Gainsboro; chart.Axis.X.MajorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.X.MajorGridLines.Visible = true; chart.Axis.X.MinorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.X.MinorGridLines.Color = System.Drawing.Color.LightGray; chart.Axis.X.MinorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.X.MinorGridLines.Visible = false; chart.Axis.X.TickmarkStyle = AxisTickStyle.Smart; chart.Axis.X.Visible = true; chart.Axis.X2.Labels.Font = new Font("Verdana", 7F); chart.Axis.X2.Labels.FontColor = System.Drawing.Color.Gray; chart.Axis.X2.Labels.HorizontalAlign = StringAlignment.Far; chart.Axis.X2.Labels.ItemFormatString = "<ITEM_LABEL>"; chart.Axis.X2.Labels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.X2.Labels.Orientation = TextOrientation.VerticalLeftFacing; chart.Axis.X2.Labels.SeriesLabels.Font = new Font("Verdana", 7F); chart.Axis.X2.Labels.SeriesLabels.FontColor = System.Drawing.Color.Gray; chart.Axis.X2.Labels.SeriesLabels.FormatString = ""; chart.Axis.X2.Labels.SeriesLabels.HorizontalAlign = StringAlignment.Far; chart.Axis.X2.Labels.SeriesLabels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.X2.Labels.SeriesLabels.Orientation = TextOrientation.VerticalLeftFacing; chart.Axis.X2.Labels.SeriesLabels.VerticalAlign = StringAlignment.Center; chart.Axis.X2.Labels.SeriesLabels.Visible = true; chart.Axis.X2.Labels.VerticalAlign = StringAlignment.Center; chart.Axis.X2.Labels.Visible = false; chart.Axis.X2.LineThickness = 1; chart.Axis.X2.MajorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.X2.MajorGridLines.Color = System.Drawing.Color.Gainsboro; chart.Axis.X2.MajorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.X2.MajorGridLines.Visible = true; chart.Axis.X2.MinorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.X2.MinorGridLines.Color = System.Drawing.Color.LightGray; chart.Axis.X2.MinorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.X2.MinorGridLines.Visible = false; chart.Axis.X2.TickmarkStyle = AxisTickStyle.Smart; chart.Axis.X2.Visible = false; chart.Axis.Y.Labels.Font = new Font("Verdana", 7F); chart.Axis.Y.Labels.FontColor = System.Drawing.Color.DimGray; chart.Axis.Y.Labels.HorizontalAlign = StringAlignment.Far; chart.Axis.Y.Labels.ItemFormatString = "<DATA_VALUE:00.##>"; chart.Axis.Y.Labels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.Y.Labels.Orientation = TextOrientation.Horizontal; chart.Axis.Y.Labels.SeriesLabels.Font = new Font("Verdana", 7F); chart.Axis.Y.Labels.SeriesLabels.FontColor = System.Drawing.Color.DimGray; chart.Axis.Y.Labels.SeriesLabels.FormatString = ""; chart.Axis.Y.Labels.SeriesLabels.HorizontalAlign = StringAlignment.Far; chart.Axis.Y.Labels.SeriesLabels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.Y.Labels.SeriesLabels.Orientation = TextOrientation.Horizontal; chart.Axis.Y.Labels.SeriesLabels.VerticalAlign = StringAlignment.Center; chart.Axis.Y.Labels.SeriesLabels.Visible = true; chart.Axis.Y.Labels.VerticalAlign = StringAlignment.Center; chart.Axis.Y.Labels.Visible = true; chart.Axis.Y.LineThickness = 1; chart.Axis.Y.MajorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.Y.MajorGridLines.Color = System.Drawing.Color.Gainsboro; chart.Axis.Y.MajorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.Y.MajorGridLines.Visible = true; chart.Axis.Y.MinorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.Y.MinorGridLines.Color = System.Drawing.Color.LightGray; chart.Axis.Y.MinorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.Y.MinorGridLines.Visible = false; chart.Axis.Y.TickmarkInterval = 10D; chart.Axis.Y.TickmarkStyle = AxisTickStyle.Smart; chart.Axis.Y.Visible = true; chart.Axis.Y2.Labels.Font = new Font("Verdana", 7F); chart.Axis.Y2.Labels.FontColor = System.Drawing.Color.Gray; chart.Axis.Y2.Labels.HorizontalAlign = StringAlignment.Near; chart.Axis.Y2.Labels.ItemFormatString = "<DATA_VALUE:00.##>"; chart.Axis.Y2.Labels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.Y2.Labels.Orientation = TextOrientation.Horizontal; chart.Axis.Y2.Labels.SeriesLabels.Font = new Font("Verdana", 7F); chart.Axis.Y2.Labels.SeriesLabels.FontColor = System.Drawing.Color.Gray; chart.Axis.Y2.Labels.SeriesLabels.FormatString = ""; chart.Axis.Y2.Labels.SeriesLabels.HorizontalAlign = StringAlignment.Near; chart.Axis.Y2.Labels.SeriesLabels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.Y2.Labels.SeriesLabels.Orientation = TextOrientation.Horizontal; chart.Axis.Y2.Labels.SeriesLabels.VerticalAlign = StringAlignment.Center; chart.Axis.Y2.Labels.SeriesLabels.Visible = true; chart.Axis.Y2.Labels.VerticalAlign = StringAlignment.Center; chart.Axis.Y2.Labels.Visible = false; chart.Axis.Y2.LineThickness = 1; chart.Axis.Y2.MajorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.Y2.MajorGridLines.Color = System.Drawing.Color.Gainsboro; chart.Axis.Y2.MajorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.Y2.MajorGridLines.Visible = true; chart.Axis.Y2.MinorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.Y2.MinorGridLines.Color = System.Drawing.Color.LightGray; chart.Axis.Y2.MinorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.Y2.MinorGridLines.Visible = false; chart.Axis.Y2.TickmarkInterval = 10D; chart.Axis.Y2.TickmarkStyle = AxisTickStyle.Smart; chart.Axis.Y2.Visible = false; chart.Axis.Z.Labels.Font = new Font("Verdana", 7F); chart.Axis.Z.Labels.FontColor = System.Drawing.Color.DimGray; chart.Axis.Z.Labels.HorizontalAlign = StringAlignment.Near; chart.Axis.Z.Labels.ItemFormatString = ""; chart.Axis.Z.Labels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.Z.Labels.Orientation = TextOrientation.Horizontal; chart.Axis.Z.Labels.SeriesLabels.Font = new Font("Verdana", 7F); chart.Axis.Z.Labels.SeriesLabels.FontColor = System.Drawing.Color.DimGray; chart.Axis.Z.Labels.SeriesLabels.HorizontalAlign = StringAlignment.Near; chart.Axis.Z.Labels.SeriesLabels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.Z.Labels.SeriesLabels.Orientation = TextOrientation.Horizontal; chart.Axis.Z.Labels.SeriesLabels.VerticalAlign = StringAlignment.Center; chart.Axis.Z.Labels.SeriesLabels.Visible = true; chart.Axis.Z.Labels.VerticalAlign = StringAlignment.Center; chart.Axis.Z.Labels.Visible = true; chart.Axis.Z.LineThickness = 1; chart.Axis.Z.MajorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.Z.MajorGridLines.Color = System.Drawing.Color.Gainsboro; chart.Axis.Z.MajorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.Z.MajorGridLines.Visible = true; chart.Axis.Z.MinorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.Z.MinorGridLines.Color = System.Drawing.Color.LightGray; chart.Axis.Z.MinorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.Z.MinorGridLines.Visible = false; chart.Axis.Z.TickmarkStyle = AxisTickStyle.Smart; chart.Axis.Z.Visible = false; chart.Axis.Z2.Labels.Font = new Font("Verdana", 7F); chart.Axis.Z2.Labels.FontColor = System.Drawing.Color.Gray; chart.Axis.Z2.Labels.HorizontalAlign = StringAlignment.Near; chart.Axis.Z2.Labels.ItemFormatString = ""; chart.Axis.Z2.Labels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.Z2.Labels.Orientation = TextOrientation.Horizontal; chart.Axis.Z2.Labels.SeriesLabels.Font = new Font("Verdana", 7F); chart.Axis.Z2.Labels.SeriesLabels.FontColor = System.Drawing.Color.Gray; chart.Axis.Z2.Labels.SeriesLabels.HorizontalAlign = StringAlignment.Near; chart.Axis.Z2.Labels.SeriesLabels.Layout.Behavior = AxisLabelLayoutBehaviors.Auto; chart.Axis.Z2.Labels.SeriesLabels.Orientation = TextOrientation.Horizontal; chart.Axis.Z2.Labels.SeriesLabels.VerticalAlign = StringAlignment.Center; chart.Axis.Z2.Labels.SeriesLabels.Visible = true; chart.Axis.Z2.Labels.VerticalAlign = StringAlignment.Center; chart.Axis.Z2.Labels.Visible = false; chart.Axis.Z2.LineThickness = 1; chart.Axis.Z2.MajorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.Z2.MajorGridLines.Color = System.Drawing.Color.Gainsboro; chart.Axis.Z2.MajorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.Z2.MajorGridLines.Visible = true; chart.Axis.Z2.MinorGridLines.AlphaLevel = ((byte)(255)); chart.Axis.Z2.MinorGridLines.Color = System.Drawing.Color.LightGray; chart.Axis.Z2.MinorGridLines.DrawStyle = LineDrawStyle.Dot; chart.Axis.Z2.MinorGridLines.Visible = false; chart.Axis.Z2.TickmarkStyle = AxisTickStyle.Smart; chart.Axis.Z2.Visible = false; chart.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; chart.ColorModel.AlphaLevel = ((byte)(150)); chart.ColorModel.ColorBegin = System.Drawing.Color.Pink; chart.ColorModel.ColorEnd = System.Drawing.Color.DarkRed; chart.ColorModel.ModelStyle = ColorModels.CustomLinear; } catch { } return chart; }