Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

注意:注意:在这个代码中没有什么和图表相关的逻辑,这只是一些一般的数据。我们将利用这些数据建立时间系列和
XY图表以及在接下来的话题中继续使用。

C#

// 简单用来保存虚拟销售数据的类型
public class SalesRecord
{
// 属性 public string Region { get; set; } public string Product { get; set; } public DateTime Date { get; set; } public double Revenue { get; set; }


Wiki Markup
  <span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> Expense \{ <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span>; \}   <span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> Profit  \{ <span style="color: #0000ff">get</span> \{ <span style="color: #0000ff">return</span> Revenue - Expense; \} \}
  <span style="color: #008000">// 构造器一</span>
  <span style="color: #0000ff">public</span> SalesRecord(<span style="color: #0000ff">string</span> region, <span style="color: #0000ff">double</span> revenue, <span style="color: #0000ff">double</span> expense)
  \{
    Region = region;
    Revenue = revenue;
    Expense = expense;
  \}
  <span style="color: #008000">// 构造器二</span>
  <span style="color: #0000ff">public</span> SalesRecord(DateTime month, <span style="color: #0000ff">string</span> product,    <span style="color: #0000ff">double</span> revenue, <span style="color: #0000ff">double</span> expense)   \{
    Date = month;
    Product = product;
    Revenue = revenue;
    Expense = expense;
  \}
\}
<span style="color: #008000">// 返回一个列表,每一个区域一个SalesRecord记录</span>
List<SalesRecord> GetSalesPerRegionData() \{
  <span style="color: #0000ff">var</span> data = <span style="color: #0000ff">new</span> List<SalesRecord>();   Random rnd = <span style="color: #0000ff">new</span> Random(0);   <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">string</span> region <span style="color: #0000ff">in</span> <span style="color: #800000">"North,East,West,South"</span>.Split(<span style="color: #800000">','</span>))
  \{     data.Add(<span style="color: #0000ff">new</span> SalesRecord(region, 100 + rnd.Next(1500), rnd.Next(500)));
  \}   <span style="color: #0000ff">return</span> data;
\}
<span style="color: #008000">// 返回一个列表,每一个产品一条SalesRecord记录</span>
<span style="color: #008000">// 在过去的12个月期间内</span>
List<SalesRecord> GetSalesPerMonthData()
\{
  <span style="color: #0000ff">var</span> data = <span style="color: #0000ff">new</span> List<SalesRecord>();   Random rnd = <span style="color: #0000ff">new</span> Random(0);   <span style="color: #0000ff">string</span>\[\] products = <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span>\[\] \{<span style="color: #800000">"Widgets"</span>, <span style="color: #800000">"Gadgets"</span>, <span style="color: #800000">"Sprockets"</span> \};   <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i < 12; i++)
  \{     <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">string</span> product <span style="color: #0000ff">in</span> products)
    \{
      data.Add(<span style="color: #0000ff">new</span> SalesRecord(
        DateTime.Today.AddMonths(i - 24),         product,         rnd.NextDouble() * 1000 * i,         rnd.NextDouble() * 1000 * i));       \}     \}     <span style="color: #0000ff">return</span> data;
     \}
\}
请注意,SalesData 类为Public类型。这是数据绑定所必需的。
在创建图表时,我们将遵循以下四个主要步骤:步骤步骤{*}1{*})选择图表类型:)选择图表类型:
下面的代码将清除任何现有的序列,然后设置图表类型:


C#

public Window1()
{
InitializeComponent();
// 清除当前图表
c1Chart.Reset(true);
// 设置图表类型
c1Chart.ChartType = ChartType.Bar;
}

...

C#

chart.Data.Children.Add(new XYDataSeries() {
ChartType=ChartType.Column,
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ce8d069b-8b43-4d94-91e3-4a9868133859"><ac:plain-text-body><![CDATA[ XValuesSource = new double[] {1,2,3 }, ValuesSource = new double[] {1,2,3 } }); chart.Data.Children.Add(new XYDataSeries() {

]]></ac:plain-text-body></ac:structured-macro>


Wiki Markup
       ChartType = ChartType.Line,
       XValuesSource = <span style="color: #0000ff">new</span> <span style="color: #0000ff">double</span>\[\] \{ 1, 2, 3 \},
       ValuesSource = <span style="color: #0000ff">new</span> <span style="color: #0000ff">double</span>\[\] \{ 3, 2, 1 \} \});
<span style="color: #3f529c">柱形折线图</span>
使用不同的数据系列模板,很容易生成各种图表类型组合。
该图表可以通过设置DataSeries.ChartType创建。


XAML

<c1chart:C1Chart Name="c1chart1">
<c1chart:C1Chart.Data>
<c1chart:ChartData >
<!-- 第一个系列的默认外观(柱形) -->
<c1chart:DataSeries Label="series 1" Values="0.5 2 3 4" />
<!-- 第二个系列中的星星符号通过折线连接 -->
<c1chart:DataSeries Label="series 2" Values="1 3 2 1"
ChartType="LineSymbols" SymbolMarker="Star4" />
</c1chart:ChartData>
</c1chart:C1Chart.Data>
</c1chart:C1Chart>

...

C#

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a97b4d2d-61e9-4c43-add9-913e9c0001ae"><ac:plain-text-body><![CDATA[

c1Chart1.Reset(true); c1Chart1.Data.Children.Add( new DataSeries() { ValuesSource = new double[] { 1, 2, 1, 3, 1, 4 }

]]></ac:plain-text-body></ac:structured-macro>


Wiki Markup
\});         c1Chart1.ChartType = ChartType.LineSymbols;
        c1Chart1.View.AxisY.ItemsSource = <span style="color: #0000ff">new</span> <span style="color: #0000ff">double</span>\[\] \{ 1.25, 1.5, 1.75, 4 \};
下面是添加前面的代码后,该图表的外观:
 \\
!worddav94c3fa0d94f34a3a6824e74b295dd9b1.png|height=275,width=345!
 \\


C#

c1Chart1.Reset(true);
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ff8f4ef4-afcf-409c-84db-826fe1e6a519"><ac:plain-text-body><![CDATA[ c1Chart1.Data.Children.Add( new DataSeries() { ValuesSource = new double[] { 1, 2, 1, 3, 1, 4 }
]]></ac:plain-text-body></ac:structured-macro>
}); c1Chart1.ChartType = ChartType.LineSymbols;
c1Chart1.View.AxisY.ItemsSource = new List<KeyValuePair<object,double>>
{ new KeyValuePair<object,double>("Min=1", 1), new KeyValuePair<object,double>("Average=2.5", 2.5), new KeyValuePair<object,double>("Max=4", 4)};

...