注意:我使用的是 .NET 6,它在 Windows 窗体中没有图表。我正在使用 winforms-datavisualization。
我有一个项目,可以分析文本文件中每个字母的频率并用它创建直方图。当我尝试使用排序字典绘制图表时出现问题。
使用
chart1.Series.Add(series);
var keys = sortedFrequencies.Keys.ToList();
var values = sortedFrequencies.Values.ToList();
for (int i = 0; i < keys.Count; i++) {
int pointIndex = series.Points.AddXY(keys[i], values[i]);
series.Points[pointIndex].AxisLabel = keys[i].ToString();
textBox1.Text += keys[i].ToString();
textBox2.Text += values[i].ToString();
}
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
chart1.Invalidate();
当我将
var keys = sortedFrequencies.Keys.ToList();
更改为
var keys = sortedFrequencies.Keys.Select(k => k.ToString()).ToList();
完整代码:
private Dictionary<char, int> charFrequencies;
public Form1() {
InitializeComponent();
charFrequencies = new Dictionary<char, int>();
}
private void chart1_Click(object sender, EventArgs e) { }
private void buttonLoadFile_Click_1(object sender, EventArgs e) {
OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == DialogResult.OK) {
textBoxFilePath.Text = fileDialog.FileName;
AnalyzeFile(fileDialog.FileName);
DrawHistogram(charFrequencies); // Показати початкові дані
}
}
private void AnalyzeFile(string filePath) {
charFrequencies.Clear();
string text = File.ReadAllText(filePath).ToLower();
foreach (char c in text) {
if (char.IsLetter(c)) {
if (charFrequencies.ContainsKey(c)) {
charFrequencies[c]++;
} else {
charFrequencies[c] = 1;
}
}
}
}
private void DrawHistogram(Dictionary<char, int> sortedFrequencies) {
chart1.Series.Clear();
Series series = new Series {
Name = "Frequency",
IsVisibleInLegend = false,
ChartType = SeriesChartType.Column
};
textBox1.Clear();
textBox2.Clear();
chart1.Series.Add(series);
var keys = sortedFrequencies.Keys.ToList();
var values = sortedFrequencies.Values.ToList();
for (int i = 0; i < keys.Count; i++) {
int pointIndex = series.Points.AddXY(keys[i], values[i]);
series.Points[pointIndex].AxisLabel = keys[i].ToString();
textBox1.Text += keys[i].ToString();
textBox2.Text += values[i].ToString();
}
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
chart1.Invalidate();
}
private Dictionary<char, int> SortAlphabetically() {
return charFrequencies.OrderBy(pair => pair.Key).ToDictionary(pair => pair.Key, pair => pair.Value);
}
private Dictionary<char, int> SortByFrequencyDesc() {
return charFrequencies.OrderByDescending(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);
}
private Dictionary<char, int> SortByFrequencyAsc() {
return charFrequencies.OrderBy(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);
}
private void Form1_Load(object sender, EventArgs e) { }
private void button1_Click(object sender, EventArgs e) {
DrawHistogram(SortAlphabetically());
}
private void button2_Click(object sender, EventArgs e) {
DrawHistogram(SortByFrequencyDesc());
}
private void button3_Click(object sender, EventArgs e) {
DrawHistogram(SortByFrequencyAsc());
}
private void textBoxFilePath_TextChanged(object sender, EventArgs e) { }
}
我在本地重现了您的问题并使用以下代码解决了它:
for (int i = 0; i < keys.Count; i++)
{
int pointIndex = series.Points.AddXY(i, values[i]); //Note "AddXY(i, "
series.Points[pointIndex].AxisLabel = keys[i].ToString();
}