私はC#
で簡単な棒グラフをペイントしようとしていますが、グラフィックスと描画の名前空間を試したことはありません。私は "開始"と "終了"のグラフィックを生成し、それから何とか(長さを表示するために)イメージを繰り返すことを考えましたが、どうやってこれを行うのか分かりません。C#:自身の棒グラフをペイント
正しい方向に私を指すことができ、そして/またはこれを行うためのサンプルコードがある場合、私は本当にうれしいでしょう。
私はC#
で簡単な棒グラフをペイントしようとしていますが、グラフィックスと描画の名前空間を試したことはありません。私は "開始"と "終了"のグラフィックを生成し、それから何とか(長さを表示するために)イメージを繰り返すことを考えましたが、どうやってこれを行うのか分かりません。C#:自身の棒グラフをペイント
正しい方向に私を指すことができ、そして/またはこれを行うためのサンプルコードがある場合、私は本当にうれしいでしょう。
私はErosに同意する必要があります。あなたが望むものを達成するための非常に優れたグラフ作成ライブラリがたくさんあります。最高のは、私が遭遇しました:
なぜあなたはhttp://zedgraph.org/wiki/index.php?title=Main_Pageを試してみてください。独自のグラフコントロールを構築する(そしてテストする)ために必要な時間は多すぎる
単純なループを使用して何が悪いですか?
アレックスは、ここではあなたを始めるための非常に簡単な例です。コードをテストするには、フォームにパネルコントロールを追加し、パネルコントロールを作成します。 (デザイナーのパネルをダブルクリックするとデフォルトで実行されます)。次に、ハンドラーコードを次のコードに置き換えます。
このコードでは、パネルに任意の長さの5つのバーが描画され、バーの幅と高さはパネルの幅と高さに関連しています。このコードは任意ですが、.Net描画機能を導入するには良い、簡単な方法です。
void Panel1Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int objCount = 5;
for (int n=0; n<objCount; n++)
{
g.FillRectangle(Brushes.AliceBlue, 0, n*(panel1.Height/objCount),
panel1.Width/(n+1), panel1.Height/objCount);
g.DrawRectangle(new Pen(Color.Black), 0, n*(panel1.Height/objCount),
panel1.Width/(n+1), panel1.Height/objCount);
g.DrawString(n.ToString(), new Font("Arial", 10f), Brushes.Black,
2, 2+n*(panel1.Height/objCount));
}
}
Paul Sasikからの返信に基づいて、少し単純な棒グラフを作成しました。これが他の人に役立つことを望む。
void DrawTheBar(int[] Values, Panel p)
{
//configuration of the bar chart
int barWidth = 20; //Width of the bar
int barSpace = 5; //Space between each bars.
int BCS = 5; //Background cell size
float fontSize = 8;
string fontFamily = "Arial";
Color bgColor = Color.White;
Brush fillColor = Brushes.CadetBlue;
Color borderColor = Color.Black;
Color bgCellColor = Color.Silver;
Brush textColor = Brushes.Black;
//do some magic here...
p.BackColor = bgColor; //set the bg color on panel
Graphics g = p.CreateGraphics();
g.Clear(bgColor); //cleans up the previously draw pixels
int Xpos = barSpace; //initial position
int panelHeight = panel1.Height-1; //fix panel height for drawing border
//Drawing rectangles for background.
for(int c = 0; c < Convert.ToInt16(panel1.Width/BCS); c++)
{
for(int r = 0; r < Convert.ToInt16(panel1.Height/BCS); r++)
{
g.DrawRectangle(new Pen(bgCellColor), c * BCS, r * BCS, BCS, BCS);
}
}
//Drawing the bars
for(int i = 0; i < Values.Length; i++)
{
//Drawing a filled rectangle. X = Xpos; Y = ((panel Height - 1) - Actual value); Width = barWidth, Height = as value define it
g.FillRectangle(fillColor, Xpos, (panelHeight - Values[i]), barWidth, Values[i]);
//Draw a rectangle around the previously created bar.
g.DrawRectangle(new Pen(borderColor), Xpos, (panelHeight - Values[i]), barWidth, Values[i]);
//Draw values over each bar.
g.DrawString(Values[i].ToString(), new Font(fontFamily, fontSize), textColor, (Xpos + 2), (panelHeight - Values[i]) - (fontSize + barSpace));
//calculate the next X point for the next bar.
Xpos += (barWidth + barSpace);
}
//here you will be happy with the results. :)
}