【问题标题】:MS Chart in WPF, Setting the DataSource is not creating the SeriesWPF中的MS图表,设置数据源不是创建系列
【发布时间】:2010-06-04 18:38:05
【问题描述】:

我在这里尝试分配数据源(使用示例应用程序中给出的相同代码)并创建一个图表,唯一的区别是我在 WPF WindowsFormsHost 中执行此操作。由于某种原因,未正确分配数据源,我无法看到正在创建的系列(“系列 1”)。连线的事情是它在 Windows 窗体应用程序中工作,但在 WPF 应用程序中不工作。

我错过了什么,有人可以帮助我吗?

谢谢

<Window x:Class="SEDC.MDM.WinUI.WindowsFormsHostWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
  xmlns:CHR="clr- namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.Dat  aVisualization"
  Title="HostingWfInWpf" Height="230" Width="338">
  <Grid x:Name="grid1">
  </Grid>
  </Window>


private void drawChartDataBinding()
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
string fileNameString = @"C:\Users\Shaik\MSChart\WinSamples\WinSamples\data\chartdata.mdb";

// initialize a connection string 
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;

// define the database query 
string mySelectQuery = "SELECT * FROM REPS;";

// create a database connection object using the connection string 
OleDbConnection myConnection = new OleDbConnection(myConnectionString);

// create a database command on the connection using query 
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
Chart Chart1 = new Chart();
// set chart data source
Chart1.DataSource = myCommand;

// set series members names for the X and Y values 
Chart1.Series"Series 1".XValueMember = "Name";
Chart1.Series"Series 1".YValueMembers = "Sales";

// data bind to the selected data source
Chart1.DataBind();

myCommand.Dispose();
myConnection.Close();
host.Child = Chart1;
this.grid1.Children.Add(host); 
} 

谢克

【问题讨论】:

  • 我已经多年没有使用 MS Chart,希望 MS Chart 专家能帮助您。您是否尝试过 CodePlex 上免费 WPFToolkit 中的 DataVisualizationToolkit?它在某些方面比 MS Chart 先进得多,而且它当然更适合 WPF。
  • 谢谢你的回答,我试过了,但性能不是很好,而且随机锁定,而且没有经过验证。我想尝试 MSChart 的原因是 MS 从 Duddas 那里买的。 amCharts 看起来很有前途,而且也很便宜

标签: wpf mschart


【解决方案1】:

通过以下两项更改,您可以修复您的代码:

1 - 代替

Chart1.Series"Series 1".XValueMember = "Name"; 
Chart1.Series"Series 1".YValueMembers = "Sales";

Chart1.Series["Series 1"].XValueMember = "Name"; 
Chart1.Series["Series 1"].YValueMembers = "Sales";

2 - 在上述代码之前,插入以下行:

Chart1.ChartAreas.Add("Default");
Chart1.Series.Add(new Series("Series 1"));

【讨论】:

  • 感谢您的回答,首先我在粘贴代码时犯了一个错误,它将“[”复制为双引号。任何手动添加图表区域和系列的方式都可以,之前对我来说很有效,但是设置数据源应该自动创建图表区域和系列,而不是。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
相关资源
最近更新 更多