我目前正在制作此图表(抱歉这是我的第一篇文章,所以我的声誉无法直接发布图片):
对于给定时间,例如10h,我的Y值表示10h到11h之间的请求数。
正如您在我的图表中看到的那样,列以X标签为中心。我的问题非常简单,如何将标签放在列的左侧,因此在我的图表中,所有列都将位于两个标签之间。
在简历中,我正在寻找一种方法在每一列上执行此操作:
如果你需要这个,这里是我的代码关于这个图的相关行
myAdapter.Fill(DailyData);
// Add points to the series
for (int i = 0; i < DailyData.Rows.Count; i++)
{
DataRow row = DailyData.Rows[i];
if (int.Parse(row["Hours"].ToString()) < 10)
{
DailyChart.Series["Series1"].Points.AddXY("0" + row["Hours"].ToString() + "h", row["RequestsNumber"].ToString());
}
else
{
DailyChart.Series["Series1"].Points.AddXY(row["Hours"].ToString() + "h", row["RequestsNumber"].ToString());
}
}
// Set series chart type
DailyChart.Series["Series1"].ChartType = SeriesChartType.Column;
DailyChart.Series["Series1"]["PointWidth"] = "1";
// Set X axis labels format
DailyChart.ChartAreas["ChartArea1"].AxisX.Interval = 1;
祝你有个美好的一天!
答案 0 :(得分:1)
我知道Microsoft的WinFormsChartSample为MSChart提供了一个名为HistogramHelper.vb
的文件,如果我没记错,可以解决您的问题。
我个人使用此代码的略微修改版本,我将在此处添加:
<强> HistogramHelper.vb 强>
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms.DataVisualization.Charting
Imports System.Collections
'--------------------------------------------------------------------------------------
'Code extracted from the WinFormsChartSample from Microsoft
'Modified to only use a single Series obj, instead of two with one of them hidden
'Also removed out the Auto Labels and % Axis
'--------------------------------------------------------------------------------------
''' <summary>
''' Helper class that creates a histogram chart. Histogram is a data
''' distribution chart which shows how many values, from the data series,
''' are inside each segment interval.
'''
''' You can define how many intervals you want to have using the SegmentIntervalNumber
''' field or the exact length of the interval using the SegmentIntervalWidth
''' field. Actual segment interval number can be slightly different due
''' to the automatic interval rounding.
''' </summary>
Public Class HistogramChartHelper
#Region "Fields"
''' <summary>
''' Number of class intervals the data range is devided in.
''' This property only has affect when "SegmentIntervalWidth" is
''' set to double.NaN.
''' </summary>
Public SegmentIntervalNumber As Integer = 20
''' <summary>
''' Histogram class interval width. Setting this value to "double.NaN"
''' will result in automatic width calculation based on the data range
''' and number of required interval specified in "SegmentIntervalNumber".
''' </summary>
Public SegmentIntervalWidth As Double = Double.NaN
''' <summary>
''' Indicates that percent frequency should be shown on the right axis
''' </summary>
Public ShowPercentOnSecondaryYAxis As Boolean = True
#End Region ' Fields
#Region "Methods"
''' <summary>
''' Creates a histogram chart.
''' </summary>
''' <param name="chartControl">Chart control reference.</param>
''' <param name="dataPoints">Original Data Series</param>
''' <param name="histogramSeriesName">Name of the histogram series.</param>
Public Function CreateHistogram(ByVal chartControl As Chart, ByVal dataPoints() As Double, ByVal histogramSeriesName As String) As Series
' Validate input
If chartControl Is Nothing Then
Debug.Print("Invalid chart control passed")
Return Nothing
End If
Dim aSeries As New Series
' Set new series chart type and other attributes
aSeries.ChartType = SeriesChartType.Column
aSeries.BorderColor = Color.Black
aSeries.BorderWidth = 1
aSeries.BorderDashStyle = ChartDashStyle.Solid
' Get data series minimum and maximum values
Dim minValue As Double = Double.MaxValue
Dim maxValue As Double = Double.MinValue
Dim pointCount As Integer = 0
For i As Integer = 0 To dataPoints.Length - 1
' Process only non-empty data points
If dataPoints(i) > maxValue Then
maxValue = dataPoints(i)
End If
If dataPoints(i) < minValue Then
minValue = dataPoints(i)
End If
pointCount += 1
Next
' Calculate interval width if it's not set
If Double.IsNaN(Me.SegmentIntervalWidth) Then
Me.SegmentIntervalWidth = (maxValue - minValue) / SegmentIntervalNumber
Me.SegmentIntervalWidth = RoundInterval(Me.SegmentIntervalWidth)
End If
' Round minimum and maximum values
minValue = Math.Floor(minValue / Me.SegmentIntervalWidth) * Me.SegmentIntervalWidth
maxValue = Math.Ceiling(maxValue / Me.SegmentIntervalWidth) * Me.SegmentIntervalWidth
' Create histogram series points
Dim currentPosition As Double = minValue
currentPosition = minValue
Do While currentPosition <= maxValue
' Count all points from data series that are in current interval
Dim count As Integer = 0
For i As Integer = 0 To dataPoints.Length - 1
Dim endPosition As Double = currentPosition + Me.SegmentIntervalWidth
If dataPoints(i) >= currentPosition AndAlso dataPoints(i) < endPosition Then
count += 1
' Last segment includes point values on both segment boundaries
ElseIf endPosition > maxValue Then
If dataPoints(i) >= currentPosition AndAlso dataPoints(i) <= endPosition Then
count += 1
End If
End If
Next
' Add data point into the histogram series
aSeries.Points.AddXY(currentPosition + Me.SegmentIntervalWidth / 2.0, count)
currentPosition += Me.SegmentIntervalWidth
Loop
' Adjust series attributes
'This is a "Custom Property" http://msdn.microsoft.com/en-us/library/dd456700.aspx
aSeries("PointWidth") = "1"
' Adjust chart area
Dim chartArea As ChartArea = chartControl.ChartAreas(0)
'chartArea.AxisY.Title = "Frequency"
chartArea.AxisX.Minimum = minValue
chartArea.AxisX.Maximum = maxValue
' Set axis interval based on the histogram class interval
' and do not allow more than 10 labels on the axis.
Dim axisInterval As Double = Me.SegmentIntervalWidth
Do While (maxValue - minValue) / axisInterval > 10.0
axisInterval *= 2.0
Loop
chartArea.AxisX.Interval = axisInterval
Return aSeries
End Function
''' <summary>
''' Helper method which rounds specified axsi interval.
''' </summary>
''' <param name="interval">Calculated axis interval.</param>
''' <returns>Rounded axis interval.</returns>
Friend Function RoundInterval(ByVal interval As Double) As Double
' If the interval is zero return error
If interval = 0.0 Then
Throw (New ArgumentOutOfRangeException("interval", "Interval can not be zero."))
End If
' If the real interval is > 1.0
Dim step_Renamed As Double = -1
Dim tempValue As Double = interval
Do While tempValue > 1.0
step_Renamed += 1
tempValue = tempValue / 10.0
If step_Renamed > 1000 Then
Throw (New InvalidOperationException("Auto interval error due to invalid point values or axis minimum/maximum."))
End If
Loop
' If the real interval is < 1.0
tempValue = interval
If tempValue < 1.0 Then
step_Renamed = 0
End If
Do While tempValue < 1.0
step_Renamed -= 1
tempValue = tempValue * 10.0
If step_Renamed < -1000 Then
Throw (New InvalidOperationException("Auto interval error due to invalid point values or axis minimum/maximum."))
End If
Loop
Dim tempDiff As Double = interval / Math.Pow(10.0, step_Renamed)
If tempDiff < 3.0 Then
tempDiff = 2.0
ElseIf tempDiff < 7.0 Then
tempDiff = 5.0
Else
tempDiff = 10.0
End If
' Make a correction of the real interval
Return tempDiff * Math.Pow(10.0, step_Renamed)
End Function
#End Region ' Methods
End Class
然后我有一个Helper函数来生成直方图:
Public Function hist(ByVal x As Double(), Optional ByVal bins As Integer = 10) As System.Windows.Forms.DataVisualization.Charting.Series
' HistogramChartHelper is a helper class found in the samples Utilities folder.
Dim histogramHelper As New HistogramChartHelper()
' Specify number of segment intervals
histogramHelper.SegmentIntervalNumber = bins
' Create histogram series
Dim newSeries As Series = histogramHelper.CreateHistogram(gca(), x, "Histogram")
gca().ChartAreas(0).AxisX.IsLogarithmic = False
gca().ChartAreas(0).AxisY.IsLogarithmic = False
gca().Series.Add(newSeries)
gcf().DoRefresh()
Return newSeries
End Function
尝试生成类似于你的直方图:
hist({8,8,8,8,8,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,13,13,14,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,18,18})
我得到: