I am simply trying to format the dates on my X Axis and it won't do it. It can't be this hard...
Here is the code, simply create a form and throw a chart control on it named "Chart", then paste in the code. I am using the <ITEM_LABEL:MM/dd/yyyy>, but it just ignores it.
How can I format the dates?
Thanks!
------------------------------
Imports Infragistics.UltraChart.Shared.StylesImports Infragistics.UltraChart.Resources.AppearanceImports Infragistics.UltraChart.Core.Layers
Public Class LineChart
Private dt As DataTable
Private Sub LineChart_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BuildDataTable1()
' Change Chart type to "composite" Chart.ChartType = ChartType.Composite Chart.Data.ZeroAligned = True
' Add a Chart Area Dim myChartArea As New ChartArea()
myChartArea.Bounds = New Rectangle(20, 10, 75, 80) myChartArea.BoundsMeasureType = MeasureType.Percentage
Chart.CompositeChart.ChartAreas.Add(myChartArea)
' Add the Axis to the chart Dim axisX As New AxisItem() axisX.OrientationType = AxisNumber.X_Axis axisX.DataType = AxisDataType.String axisX.SetLabelAxisType = SetLabelAxisType.ContinuousData axisX.Labels.ItemFormat = AxisItemLabelFormat.ItemLabel axisX.Labels.ItemFormatString = "<ITEM_LABEL:MM/dd/yyyy>" axisX.Labels.Layout.Padding = 30 axisX.Labels.Orientation = TextOrientation.Custom axisX.Labels.OrientationAngle = 45 axisX.Labels.Layout.Behavior = AxisLabelLayoutBehaviors.UseCollection
Dim axisY As New AxisItem() axisY.OrientationType = AxisNumber.Y_Axis axisY.DataType = AxisDataType.Numeric axisY.Labels.ItemFormatString = "<DATA_VALUE:###,###,##0.# ->" axisY.Labels.HorizontalAlign = StringAlignment.Far
myChartArea.Axes.Add(axisX) myChartArea.Axes.Add(axisY)
' Add the series data Dim series1 As New NumericSeries() series1.Label = "Walmart" series1.Data.DataSource = dt series1.Data.LabelColumn = "MONTH" series1.Data.ValueColumn = "WALMART" Chart.CompositeChart.Series.Add(series1)
Dim series2 As New NumericSeries() series2.Label = "Target" series2.Data.DataSource = dt series2.Data.LabelColumn = "MONTH" series2.Data.ValueColumn = "TARGET" Chart.CompositeChart.Series.Add(series2)
Dim series3 As New NumericSeries() series3.Label = "CVS" series3.Data.DataSource = dt series3.Data.LabelColumn = "MONTH" series3.Data.ValueColumn = "CVS" Chart.CompositeChart.Series.Add(series3)
' Add a Chart Layer Dim ChartLayer As New ChartLayerAppearance()
ChartLayer.ChartType = ChartType.LineChart
ChartLayer.ChartArea = myChartArea ChartLayer.AxisX = axisX ChartLayer.AxisY = axisY ChartLayer.Series.Add(series1) ChartLayer.Series.Add(series2) ChartLayer.Series.Add(series3) Chart.CompositeChart.ChartLayers.Add(ChartLayer)
' Add a Legend Dim myLegend As New CompositeLegend() myLegend.ChartLayers.Add(ChartLayer) myLegend.Bounds = New Rectangle(0, 10, 20, 25) myLegend.BoundsMeasureType = MeasureType.Percentage myLegend.PE.ElementType = PaintElementType.Gradient myLegend.PE.FillGradientStyle = GradientStyle.Circular myLegend.PE.Fill = Color.Cornsilk myLegend.PE.FillStopColor = Color.White myLegend.Border.CornerRadius = 10 myLegend.Border.Thickness = 0 Chart.CompositeChart.Legends.Add(myLegend)
End Sub
Private Sub BuildDataTable1()
Dim dc As DataColumn = Nothing Dim dr As DataRow = Nothing
dt = New DataTable
dc = New DataColumn("MONTH", GetType(System.DateTime)) dt.Columns.Add(dc) dc = New DataColumn("WALMART", GetType(System.Decimal)) dt.Columns.Add(dc) dc = New DataColumn("TARGET", GetType(System.Decimal)) dt.Columns.Add(dc) dc = New DataColumn("CVS", GetType(System.Decimal)) dt.Columns.Add(dc)
dr = dt.NewRow dr.Item("MONTH") = New DateTime(2011, 1, 1) dr.Item("WALMART") = 120000 dr.Item("TARGET") = 100000 dr.Item("CVS") = 77000 dt.Rows.Add(dr)
dr = dt.NewRow dr.Item("MONTH") = New DateTime(2011, 2, 1) dr.Item("WALMART") = 55000 dr.Item("TARGET") = 75000 dr.Item("CVS") = 90000 dt.Rows.Add(dr)
dr = dt.NewRow dr.Item("MONTH") = New DateTime(2011, 3, 1) dr.Item("WALMART") = 20000 dr.Item("TARGET") = 25000 dr.Item("CVS") = 35000 dt.Rows.Add(dr)
dr = dt.NewRow dr.Item("MONTH") = New DateTime(2011, 4, 1) dr.Item("WALMART") = 150000 dr.Item("TARGET") = 175000 dr.Item("CVS") = 180000 dt.Rows.Add(dr)
End Class
Thanks, Nicolay, however setting the Ticker Intervals and Type did not help since we are using DataType = "String" (we set the dates with iRenderLabel which for some reason does not get executed), so I guess the chart does not know how to space string date values since it's not Time values. Any suggestions?
Hi Aleksandr Mazo,
With Column chart you should use NumericSeries - http://help.infragistics.com/Help/NetAdvantage/ASPNET/2012.1/CLR4.0/html/Chart_Requirements_for_Series_Binding.html. And here is an article which could help you with the axis labels - http://help.infragistics.com/Help/NetAdvantage/ASPNET/2012.1/CLR4.0/html/Chart_Display_Labels_at_Intervals_on_a_Time_Scale_Axis.html.
Thanks, here is the front end and back end code (since the data is in the LINQ object, you won't be able to compile/run it). The chart is located in a custom control. Basically what happens, if I set the DataType="String" and in the back end code use
NumericSeries rollts2 = new NumericSeries();
The chart works, but the X axis labels have dates which are too close (see below)
But if I use DataType = "Time" and
NumericTimeSeries rollts = new NumericTimeSeries();
Then the dates are fine, but the chart itself does not get rendered as in the image below:
HTML Markup:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RollingRiskAttributionControl.ascx.cs" Inherits="Resources_controls_RollingRiskAttributionControl" %> <igmisc:WebPanel ID="WebPanel14" runat="server"> <Header Text="ROLLING RISK ATTRIBUTION"> <ExpandedAppearance> <Styles ForeColor="#727484" Font-Size="10px"> </Styles> </ExpandedAppearance> <HoverAppearance> <Styles BackColor="DarkGray"> </Styles> </HoverAppearance> </Header> <Template> <igchart:UltraChart ID="chRollStyle" runat="server" ChartType="Composite" EmptyChartText="Rolling Regression Data is not avaialble" Version="10.2" Width="960px" Height="300px" Visible="true"> <ColorModel AlphaLevel="150" ColorBegin="#edd9ce" ColorEnd="#c04f15" ModelStyle="LinearRange"> </ColorModel> <CompositeChart> <Legends> <igchartprop:CompositeLegend Visible="true"> <Border Thickness="0" /> </igchartprop:CompositeLegend> </Legends> <ChartLayers> <igchartprop:ChartLayerAppearance AxisXKey="axis1" AxisYKey="axis2" ChartAreaKey="area1" ChartType="StackColumnChart" Key="chartLayer1" SwapRowsAndColumns="True"> <ChartTypeAppearances> </ChartTypeAppearances> </igchartprop:ChartLayerAppearance> </ChartLayers> <ChartAreas> <igchartprop:ChartArea Key="area1"> <Axes> <igchartprop:AxisItem DataType="String" Extent="50" Key="axis1" OrientationType="X_Axis" SetLabelAxisType="GroupBySeries" TickmarkInterval="0" LineThickness="1"> <MajorGridLines AlphaLevel="255" Color="Gainsboro" DrawStyle="Dot" Thickness="1" Visible="True" /> <MinorGridLines AlphaLevel="255" Color="LightGray" DrawStyle="Dot" Thickness="1" Visible="False" /> <Labels HorizontalAlign="Near" ItemFormatString="<DATA_VALUE:MMM-yy>" Orientation="VerticalLeftFacing" VerticalAlign="Center"> <SeriesLabels HorizontalAlign="Center" Orientation="VerticalLeftFacing" VerticalAlign="Center"> </SeriesLabels> </Labels> </igchartprop:AxisItem> <igchartprop:AxisItem DataType="Numeric" Extent="120" Key="axis2" OrientationType="Y_Axis" SetLabelAxisType="GroupBySeries" TickmarkInterval="0" LineThickness="1"> <MajorGridLines AlphaLevel="255" Color="Gainsboro" DrawStyle="Dot" Thickness="1" Visible="True" /> <MinorGridLines AlphaLevel="255" Color="LightGray" DrawStyle="Dot" Thickness="1" Visible="False" /> <Labels HorizontalAlign="Near" ItemFormatString="<DATA_VALUE:0.00%>" Orientation="Horizontal" VerticalAlign="Center"> <SeriesLabels HorizontalAlign="Center" Orientation="Horizontal" VerticalAlign="Center"> </SeriesLabels> </Labels> </igchartprop:AxisItem> </Axes> <Border Thickness="0" /> <GridPE ElementType="None" /> </igchartprop:ChartArea> </ChartAreas> <Legends> <igchartprop:CompositeLegend ChartLayerList="chartLayer1" Bounds="0, 0, 0, 0" BoundsMeasureType="Percentage" LabelStyle-FontSizeBestFit="true" LabelStyle-Orientation="Horizontal" Border-Color="Silver" Border-Thickness="1" Border-DrawStyle="Dash"> <Border Thickness="0" /> </igchartprop:CompositeLegend> </Legends> </CompositeChart> <Border Thickness="0" /> <Axis> <PE ElementType="None" Fill="Cornsilk" /> <X TickmarkInterval="0" Visible="True" Extent="100"> <MajorGridLines AlphaLevel="255" Color="Gainsboro" DrawStyle="Dot" Thickness="1" Visible="True" /> <MinorGridLines AlphaLevel="255" Color="LightGray" DrawStyle="Dot" Thickness="1" Visible="False" /> <Labels HorizontalAlign="Near" ItemFormatString="<ITEM_LABEL>" Orientation="VerticalLeftFacing" VerticalAlign="Center"> <SeriesLabels FormatString="" HorizontalAlign="Near" Orientation="VerticalLeftFacing" VerticalAlign="Center"> </SeriesLabels> </Labels> </X> <Y TickmarkInterval="0" Visible="True"> <MajorGridLines AlphaLevel="255" Color="Gainsboro" DrawStyle="Dot" Thickness="1" Visible="True" /> <MinorGridLines AlphaLevel="255" Color="LightGray" DrawStyle="Dot" Thickness="1" Visible="False" /> <Labels HorizontalAlign="Far" ItemFormatString="<DATA_VALUE:0.00%>" Orientation="Horizontal" VerticalAlign="Center"> <SeriesLabels FormatString="" HorizontalAlign="Far" Orientation="Horizontal" VerticalAlign="Center"> </SeriesLabels> </Labels> </Y> </Axis> <Effects> <Effects> <igchartprop:GradientEffect /> </Effects> </Effects> <Data SwapRowsAndColumns="True"> </Data> <Border Color="" /> <Tooltips Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" /></igchart:UltraChart> </Template> </igmisc:WebPanel>
Backend Code:
public partial class Resources_controls_RollingRiskAttributionControl : TAReportControl, Infragistics.UltraChart.Resources.IRenderLabel{ protected void Page_Load(object sender, EventArgs e) { if (this.Height != null) { chRollStyle.Height = this.Height; } if (this.Width != null) { chRollStyle.Width = this.Width; } string watermark = "~/images/SquareTiles_BG.gif"; if (!string.IsNullOrEmpty(Request.QueryString["watermark"])) { watermark = Request.QueryString["watermark"].ToString(); } chRollStyle.BackgroundImageFileName = Server.MapPath(watermark); chRollStyle.Legend.BackgroundColor = Color.White; chRollStyle.ColorModel.ModelStyle = Infragistics.UltraChart.Shared.Styles.ColorModels.CustomLinear; chRollStyle.ColorModel.CustomPalette = new Color[] { Color.FromArgb(105, 105, 105), Color.FromArgb(205, 55, 5), Color.FromArgb(142, 137, 59), Color.FromArgb(140, 112, 127), Color.FromArgb(205, 105, 5), Color.FromArgb(126, 144, 132), Color.FromArgb(157, 46, 5), Color.FromArgb(105, 55, 5), Color.FromArgb(205, 155, 56) }; } private string _key; public string Key { get { if (string.IsNullOrEmpty(_key)) { _key = "ROLLFACTOR"; } return _key; } set { _key = value; } } protected override void PopulateControl(ReportData data) { RollingFactorAnalysis rfa = data[Key] as RollingFactorAnalysis; this.chRollStyle.CompositeChart.Series.Clear(); this.Session.Add("XL_" + Key, rfa); if (rfa != null) { int N = rfa.RollingCRISK.GetLength(1); int T = rfa.RollingCRISK.GetLength(0); for (int j = 0; j < N; j++) { IPriceable _factor = rfa.Factors[j]; NumericTimeSeries rollts = new NumericTimeSeries(); NumericSeries rollts2 = new NumericSeries(); rollts.Label = _factor.Name; for (int i = 0; i < rfa.Dates.Count; i++) { DateTime dt = rfa.Dates[i]; rollts.Points.Add(new NumericTimeDataPoint(dt, rfa.RollingCRISK[i, j], dt.ToString("MMM-yy"), false)); rollts2.Points.Add(new NumericDataPoint(rfa.RollingCRISK[i, j], dt.ToString("MMM-yy"), false)); } chRollStyle.CompositeChart.ChartLayers[0].Series.Add(rollts); //chRollStyle.CompositeChart.ChartLayers[0].Series.Add(rollts2); } System.Collections.Hashtable hash = new System.Collections.Hashtable(); hash.Add("RISKAI", new RollingChartTooltip()); //hash.Add("XAXIS", this); chRollStyle.Tooltips.Display = TooltipDisplay.MouseClick; chRollStyle.Tooltips.Format = TooltipStyle.Custom; chRollStyle.Tooltips.FormatString = "<RISKAI>"; chRollStyle.LabelHash = hash; //this.chRollStyle.ResetAxis(); //this.chRollStyle.Axis.X.Labels.SeriesLabels.Format = Infragistics.UltraChart.Shared.Styles.AxisSeriesLabelFormat.Custom; //this.chRollStyle.Axis.X.Labels.ItemFormatString = "<XAXIS>"; //chRollStyle.CompositeChart.ChartLayers[0].AxisX.Labels.ItemFormat = AxisItemLabelFormat.Custom; //chRollStyle.CompositeChart.ChartLayers[0].AxisX.Labels.ItemFormatString = "<XAXIS>"; //chRollStyle.CompositeChart.ChartLayers[0].ChartArea.Axes[0].Visible = true; //chRollStyle.CompositeChart.ChartLayers[0].ChartArea.Axes[0].Labels.ItemFormat = AxisItemLabelFormat.Custom; //chRollStyle.CompositeChart.ChartLayers[0].ChartArea.Axes[0].Labels.ItemFormatString = "<XAXIS>"; //chRollStyle.Axis.X.Extent = 100; //chRollStyle.Axis.Y.Extent = 50; //axisX.TimeAxisStyle.TimeAxisStyle = RulerGenre.Discrete of your AxisX //chRollStyle.CompositeChart.ChartLayers[0].AxisX.Labels.ItemFormatString = "<XAXIS>"; //chRollStyle.CompositeChart.ChartLayers[0].ChartArea.Axes[0].Visible = true; //chRollStyle.CompositeChart.ChartLayers[0].ChartArea.Axes[0].Labels.ItemFormat = AxisItemLabelFormat.Custom; //chRollStyle.CompositeChart.ChartLayers[0].ChartArea.Axes[0].Labels.ItemFormatString = "<XAXIS>"; } } int cnt = 0; public string ToString(System.Collections.Hashtable context) { return "Hello"; int window = 12; if (cnt == window) { cnt = 0; return context["ITEM_LABEL"].ToString(); } else { cnt++; return ""; } }}
Hello Aleksandr,
Could you please provide us more details about your chart. What is the chart type that you are using. What is the axis type ? Are you using Composite chart.
If you are able please upload small sample with your issue and I`ll be glad to research it for you.
Please do not hesitate to write us.
Regards
I tried switching the data type to Time, but for some reason the chart itself disappeared (although the legend and both axis are visible and look right). What would cause this sort of problem?
Thanks