[英]Decade log axis in MS Chart Control
我想創建十進制對數y軸,如第一幅圖所示。 第一張圖片的對數y軸最小值為78。 然后,它顯示其他以10的冪為單位的軸值(例如100和1000)。最后,它顯示對數y軸的最大值。第一個圖未使用MS Chart控件開發。
第二張圖中的圖形是使用MS Chart控件開發的。 在第二張圖中,類似於第一張圖,我無法以10的冪顯示軸值,例如100和1000。 我正在使用下面的代碼來創建此圖。
axis.MinorGrid.Interval = 1;
axis.MinorTickMark.Interval = 1;
axis.MajorGrid.Interval = 1;
axis.MajorTickMark.Interval = 1;
我想創建第二張圖,類似於第一張圖
經過一番努力,我找到了解決該問題的方法。 為了以10的冪顯示對數y軸值,需要為標簽設置間隔偏移。 也可以相應地設置主網格和次網格。 例如,在圖片2中,最小值為78.53。 因此,如果要顯示100,則需要將偏移設置為21.47。 要計算偏移量,我必須執行計算
100-78.53 = 21.47。
現在,由於我們正在處理y軸的對數值,因此我們需要以對數值進行此計算
偏移量= log(100)-log(78.53)
如果將圖片2中的上述偏移量設置為軸標簽樣式,主網格和次網格,則第一個標簽和網格將從100開始,然后根據需要從1000、100000開始。 標簽樣式,主要和次要網格的間隔應設置為1,表示1個十年。
現在,第二個問題是如何顯示圖形的最小值和最大值。 默認情況下,MS Chart將不顯示這些值。 我們需要處理自定義標簽以顯示最小值和最大值以及介於10、100、1000等之間的值,因為如果添加自定義標簽,則需要我們自己處理所有軸的標簽。
下面是創建上述所需圖形的代碼。
chart1.Series.Clear();
chart1.ChartAreas[0].AxisY.CustomLabels.Clear();
chart1.Series.Add("FirstSeries");
// Set the type to line
chart1.Series["FirstSeries"].ChartType = SeriesChartType.Line;
// Color the line of the graph light green and give it a thickness of 3
chart1.Series[0].Color = Color.Red;
chart1.Series[0].BorderWidth = 1;
int minimum = 7, maximum = 300;
chart1.ChartAreas[0].AxisY.Minimum = minimum;
chart1.ChartAreas[0].AxisY.Maximum = maximum;
double logMin = Math.Log(chart1.ChartAreas[0].AxisY.Minimum, 10);
//If minimum starts from log(7) = 0.845 then take 1 which is equivalent to 100 in normal mode.
double ceilMin = Math.Ceiling(logMin);
double logMax = Math.Log(chart1.ChartAreas[0].AxisY.Maximum, 10);
chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
chart1.ChartAreas[0].AxisY.MinorGrid.Enabled = true;
chart1.ChartAreas[0].AxisY.MajorTickMark.Enabled = true;
chart1.ChartAreas[0].AxisY.MinorTickMark.Enabled = true;
double intervalOffset = ceilMin - logMin;
chart1.ChartAreas[0].AxisY.LabelStyle.IntervalOffset = intervalOffset;
chart1.ChartAreas[0].AxisY.MajorGrid.IntervalOffset = intervalOffset;
chart1.ChartAreas[0].AxisY.MinorGrid.IntervalOffset = intervalOffset;
chart1.ChartAreas[0].AxisY.MajorTickMark.IntervalOffset = intervalOffset;
chart1.ChartAreas[0].AxisY.MinorTickMark.IntervalOffset = intervalOffset;
//Setting intervals
chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 1;
chart1.ChartAreas[0].AxisY.MajorTickMark.Interval = 1;
chart1.ChartAreas[0].AxisY.MinorGrid.Interval = 1;
chart1.ChartAreas[0].AxisY.MinorTickMark.Interval = 1;
chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 1;
for (int x = minimum; x <= maximum; x += 1)
{
chart1.Series[0].Points.Add(x, x);
}
//Adding custom labels for powers of 10 for example 1,10,100
for (double x = minimum; x <= maximum; x+=1)
{
double logX = Math.Log10(x);
double floorX = Math.Floor(logX);
if (logX - floorX == 0)
chart1.ChartAreas[0].AxisY.CustomLabels.Add(logX-0.05, logX + 0.05, "" + Math.Pow(10, logX), 0, LabelMarkStyle.None);
}
//Adding minimum and maximum label. 0.05 factor is added by experiment.
chart1.ChartAreas[0].AxisY.CustomLabels.Add(logMin-0.05, logMin + 0.05, "" + chart1.ChartAreas[0].AxisY.Minimum, 0, LabelMarkStyle.None);
chart1.ChartAreas[0].AxisY.CustomLabels.Add(logMax-0.05, logMax+0.05, "" + chart1.ChartAreas[0].AxisY.Maximum, 0, LabelMarkStyle.None);
chart1.ChartAreas[0].AxisY.IsLogarithmic = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.