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. Image for the cookie policy date

IF statement within series definition

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

5 Replies

JA Jayavigneshwaran Syncfusion Team 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 



JH John Hind 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


JA Jayavigneshwaran Syncfusion Team 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 



JH John Hind 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


DD Dharanidharan Dharmasivam Syncfusion Team 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. 


Loader.
Up arrow icon