• Home
  • About
  • Resume

Global Chart Type for Dundas Chart Control

By , 05/19/2009 11:52 AM

      Dundas Charts for .NET has many cool features like async callbacks, AJAX scroll and zoom, plenty of chart types and great visuals, but in some cases some of those features could turn out to be more of a burden than useful. One example – the ability to set chart type for each individual series of a chart. This offers great flexibility in building combination charts (you can display column and line on the same chart to reflect different series’ data), but you’re losing the Infragistics simplicity of just setting the chart type and binding chart to data without worrying about appearance of each individual series.
      This is especially inconvinient if you load chart appearance (including series appearance and chart type) from an external template. If you want to use chart type from the template, but feed it your own data – you’re stuck.  But there is a solution. After template is loaded – save the first series into a local variable (clear its data points, leaving appearance only) and clear the entire series collection:

Private Sub SaveSeriesAppearance()
     If m_dcDundasChart.Series.Count > 0 Then
         m_dcDundasChart.Series(0).Points.Clear() 'clearing all series data
         m_dcSeriesTemplate = m_dcDundasChart.Series(0)  'remembering series appearance
         m_dcDundasChart.Series.Clear() 'clearing chart series
     Else
         m_dcSeriesTemplate = New Series
         m_dcSeriesTemplate.Type = SeriesChartType.StackedColumn
     End If
        m_dcSeriesTemplate.Name = "SeriesTemplate"
End Sub

In this case if chart had no series – a default series template is created with default chart type – Stacked Column. After series appearance is saved you can use it when databinding your chart to the data source (here – DataView):

Public Sub BindToData(ByVal i_dvData As DataView)
  Dim oNewSeries As Series

  m_dcDundasChart.DataSource = i_dvData

  If i_dvData IsNot Nothing Then
     For I As Integer = 1 To i_dvData.Table.Columns.Count - 1

        oNewSeries = CopySeries(m_dcSeriesTemplate, i_dvData.Table.Columns(I).ColumnName)
        m_dcDundasChart.Series.Add(oNewSeries)

        With oNewSeries
            .ValueMemberX = 0
            .ValueMembersY = I
        End With
  Next
End If

During databind a new series is created by copying the series template thus carrying over chart type and any custom attributes (like drawing style) creating a uniform chart appearance. And finally the function to copy series (modified version of the one, provided by Dundas support):

Private Function CopySeries(ByVal i_oSourceSeries As Series, ByVal i_sNewSeriesName As String) As Series
  Dim oNewSeriers As New Series(i_sNewSeriesName)

  For Each oProp As PropertyDescriptor In TypeDescriptor.GetProperties(i_oSourceSeries)
     'if property is readonly or if it is series name - do not do anything
     If oProp.IsReadOnly OrElse oProp.Name = "Name" Then Continue For

     'otherwise copy propery
     oProp.SetValue(oNewSeriers, oProp.GetValue(i_oSourceSeries))
  Next

  Return oNewSeriers
End Function

It accepts 2 parameters, the source series and the name for the new series, creates a blank new series and copies all properties except “Name” and read-only ones from the source series.

2 Responses to “Global Chart Type for Dundas Chart Control”

  1. dkw says:

    How does it compare to the free charting package called Style Chart at http://chart.inetsoft.com.Examples there look very impressive. And it’s free!

  2. Yuriy says:

    Looks interesting, I’d be vary of purely client-side model.

Leave a Reply

Panorama Theme by Themocracy