Reputation: 147
There is a code for the 4 most frequently occurring word
string words = "One one Two two three four".ToLower();
var results = words
.Split(' ')
.Where(x => x.Length > 3)
.GroupBy(x => x)
.Select(x => new {
Count = x.Count(),
Word = x.Key })
.OrderByDescending(x => x.Count)
.Take(3);
foreach (var item in results)
{
MessageBox.Show(String.Join("\n", results.Select(x => String.Format("{0} --- {1} ", x.Word, x.Count)));
}
How can I make a chart of frequency of words like this
I know how to make chart, but I don't know how to connect it to my code
this.chart1.Series["Series1"].Points.AddXY("one", 2);
this.chart1.Series["Series1"].Points.AddXY("two", 2);
this.chart1.Series["Series1"].Points.AddXY("three", 1);
this.chart1.Series["Series1"].Points.AddXY("four", 1);
Upvotes: 1
Views: 53
Reputation: 54453
Simply like this:
foreach (var r in results )
{
chart1.Series["Series1"].Points.AddXY(r.Word, r.Count);
}
After changing the data a little:
string words = "One one Two two three four four four two two two five five".ToLower();
We get this:
Do note that this is a case of having strings as the x-values. This means that the actual x-values in the DataPoints are not those strings (they aredouble
)
but are all 0
.
Which has all sorts of implications.
One is that you can't easily add another series with points that show other values but rely on the same x-values. Nor can you use the (lost) values to set Minimum/Maximum values or format labels..
Sometimes having an additional series with the same x-values, but maybe in a different order, can be made to work by calling Chart.AlignDataPointsByAxisLabel..
Upvotes: 2