I have got an Diagram example that is supposed to draw a diagram that I have on a picture.
The code compiles but cant understand how to trigger this code for example in the button click event that I have set up.

Perheps there is any C# code basics of how to execute this code to Create a Chart.

I have done a guess of code in the button click event to execute something but it seems that .CreateChart() needs arguments inside the (). The compiler shows (ref Chart Chart1) when entering the first leftside paranthes but that doesn´t compile

Any idéas of how to execute this so the chart will be drawn. The code should be right.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
using dotnetCHARTING.WinForms;

namespace Charts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RateCharts CreateTestChart = new RateCharts();
            //CreateTestChart.CreateChart();
        }
    }




    public class RateCharts
    {


        public RateCharts()
        {
        }
        public void CreateChart(ref Chart Chart1)
        {


            Chart1.Type = ChartType.Combo;//Horizontal;
            Chart1.Width = 600;
            Chart1.Height = 350;
            Chart1.TempDirectory = "temp";

            // Rate charts can be used to show many interesting stats. For this sample we will make a chart that will show
            // click through rates for banner campaigns.

            // Lets get our data first. The methods below will generate some random data for us to use.

            Series views = RateCharts.getRandomViewsData();
            Series clicks = RateCharts.getRandomClicksData();

            // Now to get a rate is as easy as this:
            Series rate = clicks / views;

            // We need a new name for the series
            rate.Name = "CTR";


            // If we want to show the clicks, views, and rates then we need to create a new y axis because the CTR is so 
            // small that it will not be readable.
            Axis ctrAxis = new Axis("Click Through Ratio (CTR)");

            // A new number precision has be to supplied so the numbers can be seen. Otherwise we will see only 0s on the axis.
            ctrAxis.NumberPrecision = 4;
            // Lets move it on the right side also 
            ctrAxis.Orientation = dotnetCHARTING.WinForms.Orientation.Right;

            // Now to assign the ctr axis to the ctr series:
            rate.YAxis = ctrAxis;


            // In the legend the values are sums by default and that does not make sense for CTR so lets 
            // make the value in the legend an average for the ctr series.
            rate.LegendEntry.Value = "Average: %YAverage";


            // Show values on bars
            Chart1.DefaultSeries.DefaultElement.ShowValue = true;


            // Add the data.
            Chart1.SeriesCollection.Add(views);
            Chart1.SeriesCollection.Add(clicks);
            Chart1.SeriesCollection.Add(rate);

        }

        static Series getRandomViewsData() // This method will generate a series representing views for our banners
        {

            Random myR = new Random(1);

            Series s = new Series();
            s.Name = "Views";
            for (int b = 1; b < 5; b++)
            {
                Element e = new Element();
                e.Name = "Banner " + b; ;
                e.YValue = 50 + myR.Next(500);
                s.Elements.Add(e);
            }


            return s;
        }

        static Series getRandomClicksData() // This method will generate a series representing clicks for our banners
        {

            Random myR = new Random(2);

            Series s = new Series();
            s.Name = "Clicks";
            for (int b = 1; b < 5; b++)
            {
                Element e = new Element();
                e.Name = "Banner " + b; ;
                e.YValue = myR.Next(50);
                s.Elements.Add(e);
            }


            return s;
        }
    }
}

Dani AI

Generated

Short answer: your CreateChart method is declared to take a ref Chart parameter, so you must pass a Chart variable with the ref keyword — or change the method to accept a normal Chart parameter (no ref). If you dropped the dotnetCHARTING control onto the form in the designer it is already available as a field (usually Chart1), so from your button click you can call it directly.

Example call patterns:

// if the method keeps the ref parameter
CreateTestChart.CreateChart(ref Chart1);

// or (recommended) change the method to
// public void CreateChart(dotnetCHARTING.WinForms.Chart chart)
// and call:
CreateTestChart.CreateChart(Chart1);

Why prefer removing ref: Chart is a reference type, so passing it without ref still lets the method modify the same object. ref is only needed if the method intends to assign a brand new Chart instance back to the caller. In your posted CreateChart code the method only mutates the control, so removing ref makes the call simpler and less error-prone.

Other practical notes and troubleshooting:

  • If you create a Chart at runtime you must add it to the form (this.Controls.Add(chart)) and set Size/Location so it is visible. Designer-created controls are initialized in InitializeComponent, so calling from the button click is safe.
  • Confirm the dotnetCHARTING.WinForms assembly is referenced and the correct using is present; if there is another Chart type in scope, fully qualify the parameter type.
  • Check for runtime exceptions, ensure any temp/output folder is writable, and refresh the control (or call any library-specific bind/refresh API) after adding series.
  • As suggested, you do need a Chart instance; as suggested, consult the dotnetCHARTING docs for control-specific rendering/refresh requirements.

Recommended Answers

All 2 Replies

See you are using types from a dotnetCHARTING namespace, do know how these work via some sort of documentation?
I don't see some kind of ShowtheGraph method in here.

You will need to call CreateChart() using a Chart object as an argument I presume that there is a Chart described in dotnetCHARTING.WinForms that you will need to use.

I don't know what a Chart is so I can't be sure but I suspect you need to do somthing like this:

private void button1_Click(object sender, EventArgs e)        
{
    Chart myChart = new Chart();

    RateCharts CreateTestChart = new RateCharts();            
    CreateTestChart.CreateChart(ref myChart);        
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.