sticky
Chart Controls - FAQ

Tüm Yanıtlar

  • How do I hide labels for data points with zero value?

    There are a couple of ways to do this.

    1) Format the label using a custom numeric format string to hide zeroes:

    Chart1.Series(0).IsValueShownAsLabel = True
    Chart1.Series(0).LabelFormat = "{0;0;#}"

    Or with keywords:

    Chart1.Series(0).Label = "#VALY{0;0;#}"

    2) Only show labels on data points with values other than zero: (Note, for some reason the inverse of this does not work for every chart type.)

    For Each dp As DataPoint In Chart1.Series(0).Points
    	If dp.YValues(0) <> 0 Then
    		dp.IsValueShownAsLabel = True
    	End If
    Next

    Note that setting Series or DataPoint.Label overrides IsValueShownAsLabel and LabelFormat.

    02 Nisan 2012 Pazartesi 09:34
    Moderatör
  • I'm using Silverlight...

    This forum is for the Chart Controls for .NET Framework which is intended to be used with ASP.NET or Windows Forms.

    For Silverlight Charts, try the Silverlight Toolkit.

    For questions concerning the use of the toolkit, visit the Silverlight Forums.

    14 Nisan 2012 Cumartesi 09:29
    Moderatör
  • I'm using WPF...

    This forum is for the Chart Controls for .NET Framework which is intended to be used with ASP.NET or Windows Forms.

    For WPF Charts, try the WPF Toolkit.

    For questions concerning the use of the toolkit, visit the WPF Toolkit Discussions site or the WPF forum.

    If you really want to use the Windows Forms Chart Control in WPF, apparently there is a way to do it with a WindowsFormsHost control. Any questions on the usage of the WindowsFormsHost should also be asked in the WPF forum.

    For more information, see:

    14 Nisan 2012 Cumartesi 09:32
    Moderatör
  • Chart displays incorrectly when all x values are zero

    When all x values are zero, the chart control will automatically place the data points on the x axis based on their index instead of their x value. (Same as setting Series.IsXValueIndexed to true.) This is a feature in the chart controls that's intended to make charts with categorical (string) x values display properly, in which case the strings are assigned to the DataPoint.AxisLabel properties and the .XValue properties will be 0. Unfortunately it does not work well when all the x values are actually meant to be zero. Setting Series.IsXValueIndexed to false won't override this behaviour. Usually this is only a problem with scatter charts (Series.ChartType = SeriesChartType.Point.)

    One way to get around this is to add a transparent dummy data point to the series in question that has an x value other than zero.

    Dim series As Series = Chart1.Series(0)
    Dim area As ChartArea = Chart1.ChartAreas(0)
    Dim yValue as Double = series.Points(0).YValues(0) 'Make sure you already have some data in the series at this point.
    
    'Determine whether every x value is zero
    Dim onlyZeroes As Boolean = True
    
    For Each dp As DataPoint In series.Points
    	If dp.XValue <> 0 Then
    		onlyZeroes = False
    		Exit For
    	End If
    Next
    
    If onlyZeroes Then
    
    	'Set x axis min & max to something suitable
    	'(If not set, the dummy data point may affect the x axis limits)
    	area.AxisX.Minimum = -1
    	area.AxisX.Maximum = 1
    
    	'Add a transparent point at an x position other than zero
    	Dim dummyPoint As New DataPoint(1, yValue)
    	dummyPoint.Color = Color.Transparent
    	series.Points.Add(dummyPoint)
    
    End If

    If you've explicitly set the AxisX.Minimum & .Maximum somewhere (not relying on the chart control to automatically adjust the x axis limits), you can just add the dummy data point without checking for zeroes.

    Dim series As Series = Chart1.Series(0)
    Dim yValue as Double = series.Points(0).YValues(0)
    
    'Add a transparent point at an x position other than zero
    Dim dummyPoint As New DataPoint(1, yValue)
    dummyPoint.Color = Color.Transparent
    series.Points.Add(dummyPoint)

    Other workarounds include adding a very small value (Single.Epsilon for example) to any x value that is zero. In this case you may need to adjust the x axis intervals, limits and labels to ignore the addition.

    In .NET 4.5 the correct way to handle this is by setting the series IsXAxisQuantitative custom property to "true".

    18 Nisan 2012 Çarşamba 10:34
    Moderatör
  • I'm using SSRS...

    This forum is for the Chart Controls for .NET Framework which is intended to be used with ASP.NET or Windows Forms.

    For questions concerning SSRS, visit the SQL Server Reporting Services Forum.

    23 Nisan 2012 Pazartesi 10:56
    Moderatör
  • X axis does not display every label

    If using categorical x values (Strings), you'll probably want to show a label on the axis for every x value. However, by default the chart control will automatically adjust the x axis scale, label interval and font to make sure there are no overlapping labels. If there is not enough room for every label, some labels may be left out.

    Most of the time an acceptable solution is to force every label to show by setting the x axis interval to 1. (Doing this may change the label font size and/or orientation.)

    Chart1.ChartAreas(0).AxisX.Interval = 1

    For different approaches and a more detailed explanation, see Alex Gorev's blog entry; Displaying Categorical Axis Labels

    14 Ağustos 2012 Salı 09:32
    Moderatör
  • ChartDataTableHelper does not work

    The ChartDataTableHelper is a utility class from the Samples Environment. It draws a table of values underneath the chart.

    You are free to use it in your project by copying the class from the samples project folder into your own prject. (Remove the namespace definition if that's causing problems.)

    If you are using C#, you should be fine. See the usage in the samples environment.

    The VB version of the class is buggy and won't even compile. It seems it has been converted to VB from C# with some tool. Here is a working version of the "Paint Event Handling" region:

    #Region "Paint Event Handling"
    
    	''' <summary>
    	''' Chart Paint event handler.
    	''' </summary>
    	Private Sub Chart_PostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs)
    
    		If TypeOf e.ChartElement Is ChartArea Then
    
    			Dim area As ChartArea = CType(e.ChartElement, ChartArea)
    
    			' call the paint method.
    			If ChartAreas.IndexOf(area.Name) >= 0 AndAlso enabled_Renamed Then
    				PaintDataTable(sender, e)
    			End If
    
    		End If
    
    	End Sub
    
    	''' <summary>
    	''' This method does all the work for the painting of the data table.
    	''' </summary>
    	Private Sub PaintDataTable(ByVal sender As Object, ByVal e As System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs)
    
    		Dim area As ChartArea = CType(e.ChartElement, ChartArea)
    
    		' get the rect of the chart area
    		Dim rect As RectangleF = e.ChartGraphics.GetAbsoluteRectangle(area.Position.ToRectangleF())
    
    		' get the inner plot position
    		Dim elemPos As ElementPosition = area.InnerPlotPosition
    
    		' find the coordinates of the inner plot position
    		Dim x As Single = rect.X + (rect.Width / 100 * elemPos.X)
    		Dim y As Single = rect.Y + (rect.Height / 100 * elemPos.Y)
    		Dim ChartAreaBottomY As Single = rect.Y + rect.Height
    
    		' offset the bottom by the width+1 of the scrollbar if it is visible
    		If area.AxisX.ScrollBar.IsVisible() AndAlso (Not area.AxisX.ScrollBar.IsPositionedInside) Then
    			ChartAreaBottomY -= (CSng(area.AxisX.ScrollBar.Size) + 1)
    		End If
    
    		Dim width As Single = (rect.Width / 100 * elemPos.Width)
    		Dim height As Single = (rect.Height / 100 * elemPos.Height)
    
    		' find the height of the font that will be used
    		Dim axisFont As Font = area.AxisX.LabelStyle.Font
    		Dim testString As String = "ForFontHeight"
    		Dim axisFontSize As SizeF = e.ChartGraphics.Graphics.MeasureString(testString, axisFont)
    
    		' find the height of the font that will be used
    		Dim titleFont As Font = area.AxisX.TitleFont
    		testString = area.AxisX.Title
    		Dim titleFontSize As SizeF = e.ChartGraphics.Graphics.MeasureString(testString, titleFont)
    
    		Dim seriesCount As Integer = 0
    
    		' for each series that is attached to the chart area,
    		' draw some boxes around the labels in the color provided
    		Dim i As Integer
    		For i = e.Chart.Series.Count - 1 To 0 Step -1
    			If area.Name = e.Chart.Series(i).ChartArea Then
    				seriesCount += 1
    			End If
    		Next i
    
    		' now, if a box was actually drawn, then draw 
    		' the verticle lines to separate the columns of the table.
    		If seriesCount > 0 Then
    
    			Dim int As Integer = 0
    
    			Do While int < e.Chart.Series.Count
    
    				If area.Name = e.Chart.Series(int).ChartArea Then
    
    					Dim min As Double = area.AxisX.Minimum
    					Dim max As Double = area.AxisX.Maximum
    
    					' modify the min value for the current axis view
    					If area.AxisX.ScaleView.Position - 1 > min Then
    						min = area.AxisX.ScaleView.Position - 1
    					End If
    
    					' modify the max value for the currect axis view
    					If (area.AxisX.ScaleView.Position + area.AxisX.ScaleView.Size + 0.5) < max Then
    						max = area.AxisX.ScaleView.Position + area.AxisX.ScaleView.Size + 0.5
    					End If
    
    					' find the starting point that will be display.
    					' this is dependent on the current axis view.
    					' this sample assumes the same number of points in each
    					' series so always take from the zeroth series
    					Dim pointIndex As Integer = 0
    					Dim pt As DataPoint
    					For Each pt In ChartObj.Series(0).Points
    						If pt.XValue > min Then
    							Exit For
    						End If
    
    						pointIndex += 1
    					Next pt
    
    					Dim TableLegendDrawn As Boolean = False
    
    					Dim AxisValue As Double = min
    
    					Do While AxisValue < max
    
    						Dim pixelX As Single = CSng(e.ChartGraphics.GetPositionFromAxis(area.Name, AxisName.X, AxisValue))
    						Dim nextPixelX As Single = CSng(e.ChartGraphics.GetPositionFromAxis(area.Name, AxisName.X, AxisValue + 1))
    						Dim pixelY As Single = ChartAreaBottomY - titleFontSize.Height - (seriesCount * axisFontSize.Height)
    
    						Dim point1 As PointF = PointF.Empty
    						Dim point2 As PointF = PointF.Empty
    
    						' Set Maximum and minimum points
    						point1.X = pixelX
    						point1.Y = 0
    
    						' Convert relative coordinates to absolute coordinates.
    						point1 = e.ChartGraphics.GetAbsolutePoint(point1)
    						point2.X = point1.X
    						point2.Y = ChartAreaBottomY - titleFontSize.Height
    						point1.Y = pixelY
    
    						' Draw connection line
    						e.ChartGraphics.Graphics.DrawLine(New Pen(borderColor_Renamed), point1, point2)
    
    
    						point2.X = nextPixelX
    						point2.Y = 0
    						point2 = e.ChartGraphics.GetAbsolutePoint(point2)
    
    						Dim format As StringFormat = New StringFormat()
    						format.Alignment = StringAlignment.Center
    						format.LineAlignment = StringAlignment.Center
    
    						' for each series draw one value in the column
    						Dim row As Integer = 0
    						Dim ser As Series
    
    						For Each ser In ChartObj.Series
    
    							If area.Name = ser.ChartArea Then
    
    								If (Not TableLegendDrawn) Then
    									' draw the series color box 
    									e.ChartGraphics.Graphics.FillRectangle(New SolidBrush(ser.Color), x - 10, row * (axisFont.Height) + (point1.Y), 10, axisFontSize.Height)
    
    									e.ChartGraphics.Graphics.DrawRectangle(New Pen(borderColor_Renamed), x - 10, row * (axisFont.Height) + (point1.Y), 10, axisFontSize.Height)
    
    									e.ChartGraphics.Graphics.FillRectangle(New SolidBrush(tableColor_Renamed), x, row * (axisFont.Height) + (point1.Y), width, axisFontSize.Height)
    
    									e.ChartGraphics.Graphics.DrawRectangle(New Pen(borderColor_Renamed), x, row * (axisFont.Height) + (point1.Y), width, axisFontSize.Height)
    								End If
    
    								If pointIndex < ser.Points.Count Then
    									Dim label As String = ser.Points(pointIndex).YValues(0).ToString()
    									Dim textRect As RectangleF = New RectangleF(point1.X, row * (axisFont.Height) + (point1.Y + 1), point2.X - point1.X, axisFont.Height)
    									e.ChartGraphics.Graphics.DrawString(label, axisFont, New SolidBrush(area.AxisX.LabelStyle.ForeColor), textRect, format)
    								End If
    
    								row += 1
    
    							End If
    
    						Next ser
    
    						TableLegendDrawn = True
    
    						pointIndex += 1
    						AxisValue += 1
    
    					Loop
    
    					' do this only once so break!
    
    					Exit Do
    
    				End If
    
    				i += 1
    
    			Loop
    
    		End If
    
    	End Sub
    
    #End Region
    24 Eylül 2012 Pazartesi 13:06
    Moderatör