We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

IF statement within series definition

Thread ID:

Created:

Updated:

Platform:

Replies:

128312 Jan 13,2017 07:08 AM UTC Jan 18,2017 01:09 PM UTC ASP.NET MVC 5
loading
Tags: Chart
John Hind
Asked On January 13, 2017 07:08 AM UTC

Is it possible to have an IF statement when defining the series data of a chart in an MVC View ?

In the code snippet below, I would like to change the "Name" property according to the value from the database:

            .Series(Sub(ser)
                        ser.Name("XXXXXXXX")
                        ser.YName("BASE_VALUE")
                        ser.Add()
                   End Sub))

I would like to do this:

If "BASE_VALUE" > 0 Then      ' From the database
    ser.Name("High")
Else
    ser.Name("Low")
Endif


Any help appreciated,
John

Jayavigneshwaran [Syncfusion]
Replied On January 16, 2017 05:30 AM UTC

Hi John 

Thanks for using Syncfusion products. 

We have analyzed your query. Yes you can use conditional statements while defining chart series in MVC view. We have prepared a sample for this scenario and attached in the below location for your reference.  
Sample link:  
Sample
 

Code snippet: 
[VB] 
      Option Infer On 
        @ 
        If True Then 
              Dim price = 50 
        End If 
 
        (Html.EJ().Chart("container").Series(Sub(sr) 
              sr.Type(SeriesType.Line).EnableAnimation(True) 
 
              If price > 40 Then 
                    sr.Name("Price greater than 40") 
              Else 
                    sr.Name("Price less than 40") 
              End If 
 
              sr.Add() 
        End Sub).Load("chartLoad").Size(sz => sz.Height("450").Width("600"))) 
 
 
[CS] 
     @{var price = 50;} 
 
      @(Html.EJ().Chart("container") 
      .Series(sr => 
      { 
          sr.Type(SeriesType.Line).EnableAnimation(true); 
 
          if (price > 40) 
              sr.Name("Price greater than 40"); 
          else 
              sr.Name("Price less than 40"); 
           
          sr.Add(); 
      }) 
      .Load("chartLoad") 
      .Size(sz => sz.Height("450").Width("600"))) 



In the above code we have specified 50 to the variable “price”. And based on its value, name of the series is assigned. You can change this code with respect to your scenario. 
The screen shot shows the output of the attached sample. 
 
 
 
Since the price value 50 is greater than 40, so the text “Price greater than 40” is assigned to the series name. And it is shown on the legend. 

Please let us know if you have any concerns. 
 
Thanks, 
Jayavigneshwaran 


John Hind
Replied On January 16, 2017 09:45 AM UTC

Thanks for the reply.

I would like the IF statement based on the value of the YName data item, not an external value from the chart.

thanks,
John

Jayavigneshwaran [Syncfusion]
Replied On January 17, 2017 07:30 AM UTC

Hi John, 

Thank for your update. As per you requirement we have now modified the sample. Please find it from the below location. 

Code snippet 
[VB] 
 
Option Infer On 
@ 
If True Then 
    Dim count = DirectCast(ViewBag.datasource, IEnumerable(Of Object)).Count() 
End If 
 
            (Html.EJ().Chart("container").Series(Sub(sr) 
                  sr.XName("OrderID").YName("Freight").DataSource(DirectCast(ViewBag.datasource, IEnumerable(Of Object))) 
                  If count > 0 Then 
                        sr.Name("High") 
                  Else 
                        sr.Name("low") 
                  End If 
                  sr.Add() 
            End Sub).Size(sz => sz.Height("450").Width("600"))) 

[CS] 
 
@{ 
     Var count = ((IEnumerable<object>)ViewBag.datasource).Count(); 
 } 
 
    @(Html.EJ().Chart("container") 
      .Series(sr => 
      { 
          sr.XName("OrderID").YName("Freight").DataSource((IEnumerable<object>)ViewBag.datasource); 
          if (count > 0) 
              sr.Name("High"); 
          else 
              sr.Name("low"); 
          sr.Add(); 
      }) 
      .Size(sz => sz.Height("450").Width("600"))) 


In the above code we have bind the data source to chart from DB using viewbag. And we have got the count of the data source and based on that checked if the count is greater than zero then text “High” is assigned to series name else “low” will be assigned. 
And we wish to know that YName property takes string value – the field name. So we have to check the condition with datasource property only. 

Screen shot 
 

Please let us know if you have any concern. 

Thanks, 
Jayavigneshwaran 


John Hind
Replied On January 17, 2017 12:28 PM UTC

Sorry, I am not looking for the Count, that was done by you in your first example.

I need to do the IF statement based on each of the Y series data values.

John

Dharanidharan Dharmasivam [Syncfusion]
Replied On January 18, 2017 01:09 PM UTC

Hi John, 

We have analyzed your query. Now we have prepared a sample with respect to your requirement. In this with respect to yValue(Freight), we have assigned the name to series. If any of the yValue is less than zero, then series name will be low. Kindly find the code snippet below, 

ASP.NET MVC: 

for (int l = 0; l < count; l++) 
          { 
             var dataSrc = (IEnumerable<object>)ViewBag.datasource; 
             var dataEle = dataSrc.ElementAt(l); 
             if (((SyncfusionMvcApplication1.Models.OrdersView)dataEle).Freight > 0) 
                 sr.Name("High"); 
             else 
             { 
                 sr.Name("low"); 
                 break; 
              } 
          } 


Screenshot: 
 

For your reference, we have attached the sample. Kinldy find the sample from below location. 
  
And in the previous update, we have provided a sample similar to your scenario and you can modify that with respect to your requirement. 
If this is not your scenario, kinldy revert us more information along with code snippet of your query, so that we can anlayze further on your requirement and provide solution sooner. 

Thanks, 
Dharani. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;