0

enter image description here

I want get StaffId on MessageBox when I click for example on David point chart (on Blue area of David as Image shows).

I mean StaffId of clicked employee's name.

Here is my series presentation..

var series = chart1.Series.Add("Series1");
series.XValueMember = "StaffId";
series.YValueMembers = "Total";
series.Name = "Employee";

and the Linq query I am using

var result = (from u in db.Transactions
              join st in db.Users on u.StaffId equals st.UserId
              group u by u.Users.FirstName into g
              select new
              {
                  StaffId = g.Key,
                  Total = g.Count() 
              }).ToList();

chart1.DataSource = result;     
chart1.DataBind();
chart1.Show();

I tried like this but not working

private void chart1_MouseClick(object sender, MouseEventArgs e)
{
    // Totally stop here .. ;)       
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Helen Tekie
  • 515
  • 1
  • 6
  • 23

1 Answers1

1

Try MouseUp event:

private void chart1_MouseUp(object sender, MouseEventArgs e)
{
    var pointEndX = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
    var pointEndY = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
}

So the full code would be something like:

public partial class Form1 : Form
{
    class User
    {
        public string StaffId { get; set; }
        public int Total { get; set; }

    }
    public Form1()
    {
        InitializeComponent();
        chart1.Series.Clear();

        var series = chart1.Series.Add("Series1");
        series.XValueMember = "StaffId";
        series.YValueMembers = "Total";
        series.Name = "Employee";

        var users = new List<User>();
        users.Add(new User(){StaffId = "John", Total = 70});
        users.Add(new User() { StaffId = "David", Total = 81 });
        users.Add(new User() { StaffId = "Sara", Total = 81 });

        chart1.DataSource = users;
        chart1.DataBind();
        chart1.Show();
    }

    private void chart1_MouseUp(object sender, MouseEventArgs e)
    {
        var pointEndX = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);

        var list = (List<User>)chart1.DataSource;

        //round to the nearest whole number
        pointEndX = Math.Round(pointEndX, 0);

        //subtract 1 because bars start at 1 and List/Array are 0 indexed
        int index = ((int)pointEndX )- 1;

        if(index <0 || index>=list.Count)
            return;

        var user = list[index];
        MessageBox.Show(user.StaffId);
    }
}
Thomas N
  • 623
  • 1
  • 4
  • 14
  • Just out of curiosity, what are you using `if (!annotating)` for? – ikerbera Jan 03 '19 at 09:39
  • 1
    Oops, should have got rid of that. This is copied from my codebase where the user can add annotations to the chart (TextAnnotation, LineAnnotation etc) but they have to have turned on annotation mode. – Thomas N Jan 03 '19 at 09:43
  • @ThomasN Thank you for response but I want to get StaffId of David when I klick on David chart and Sara's StaffId when I click on Sara's chart part and so on as you see in Image . StaffId means UserId. What I'am geting from your code is a value of X and Y. – Helen Tekie Jan 03 '19 at 09:58
  • What value do you get in pointEndX if you put a break point in your code after it and click on David bar? – Thomas N Jan 03 '19 at 10:11
  • Well, it depends where in the area I click, right now I get 2.283674....... I'am geting a value of X – Helen Tekie Jan 03 '19 at 10:23
  • Looks like the bars are 1,2,3 as far as the control is concerned? So if you cast that to an `int` there should be a collection you can index either in the chart or in your local variable `result` (which would have to become a class level field). e.g. `result[((int)pointEndX)-1]` – Thomas N Jan 03 '19 at 10:29
  • @ThomasN where du I write this code result[((int)pointEndX)-1] ???? I realy don't get it ..;) Variable result is inside button – Helen Tekie Jan 03 '19 at 11:38
  • I've updated to include a full example. I created my own list because I don't have access to your database/classes. – Thomas N Jan 03 '19 at 12:21
  • @ThomasN thank you, but If I Exclude my database and use your hole code as it is, then I get name, not Id. – Helen Tekie Jan 03 '19 at 12:50
  • @HelenTekie My code is just an example, you should take the MouseUp code only and paste it into your own code. Then you need to cast chart1.DataSource to whatever your linq statement produces (debugger should tell you that). I.e. it won't be a `List` it will be a `List` – Thomas N Jan 03 '19 at 13:00
  • If I do as you Writing then I get error message like ... "failed to convert a system.Collections.Generic.List object [<> f_AnonymousType8'2 [System.String, System.int32]]] to the system.Collection.Generic.List [EmpReport.Users] type" – Helen Tekie Jan 03 '19 at 13:28
  • Ok so `select new` in your linq code is creating an AnonymousType. Can you instead create a class yourself? e.g. `select new MyClass {StaffId = g.Key,Total = g.Count()}).ToList();` That would get around that problem. It would basically look the same as the `User` class I created. – Thomas N Jan 03 '19 at 13:32
  • @ThomasN very strange, I get name then not Id.. ;) – Helen Tekie Jan 03 '19 at 13:39
  • @ThomasN if you look my result Queary , maybe there is something to change there – Helen Tekie Jan 03 '19 at 13:42
  • Yes you `group u by u.Users.FirstName` then assign it to `StaffId` in your linq. I don't really know linq to sql that well. Can you group by name AND StaffId since I'm guessing that's what you would want anyway. Then store all 3 in your class. – Thomas N Jan 03 '19 at 13:56
  • @ThomasN , I want to show employee name in chart then I want to click on that specific employeename and get his/her Id and show Another information like details about this clicked employee. Offcourse if I change grouped by u.Users.StaffId, then I get StaffId even by u.Users.username then I get value username. But chart part do not show names of employee. It shows Id and username. But this is a litle progress ;) Maybe it's enough if I show username instead. – Helen Tekie Jan 03 '19 at 14:56
  • Good luck. Maybe you can use the approach in https://stackoverflow.com/questions/847066/group-by-multiple-columns to select both StaffId and Username. – Thomas N Jan 03 '19 at 15:07