Note: I am using .NET 6 that does not have a chart in Windows Form. I am using winforms-datavisualization.
I have a project that analyzes a text file on the frequency of each letter and creates Histogram with it. Problems occur when I try to draw the graph with a sorted dictionary.
Using
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();
I get all columns, but they are not sorted for some reason:
And when i change var keys = sortedFrequencies.Keys.ToList();
to
var keys = sortedFrequencies.Keys.Select(k => k.ToString()).ToList();
I get what seems like a sorted graph, but all the columns overlap:
Full code:
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) { }
}
I reproduced your issue locally and resolved it by using the following code:
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();
}