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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to load information that is not hard coded in Sparkline Grid Template

Thread ID:

Created:

Updated:

Platform:

Replies:

145796 Jul 9,2019 03:51 AM UTC Jul 10,2019 10:22 AM UTC ASP.NET Core - EJ 2 1
loading
Tags: Sparkline Charts
Mark Tannehill
Asked On July 9, 2019 03:51 AM UTC

I'm basically following this example:


The basics are working, I can get a chart to show up in each one of my rows.

My question is how to attach this to data that's not hardcoded.. like the LineData?

Here's essentially my grid:

<div class="control-section">
                    <div id="sparkline" class="row">
                        <div class="cols-sample-area">
                            <script type="text/x-template" id="columnTemplate1">
                                <div id="spkline${Id}" />
                            </script>

                            <div id="grid">
                                <ejs-grid id="grid" created="loadComplete" dataSource="@Model.Measures" allowSelection="false" enableColumnVirtualization="false" enableHover="true" height="400">
                                    <e-grid-columns>
                                        <e-grid-column field="Id" headerText="ID" textAlign="Right" width="60"></e-grid-column>
                                        <e-grid-column field="MeasureName" headerText="MeasureName" width="60"></e-grid-column>
                                        <e-grid-column field="MeasureDescription" headerText="MeasureDescription" width="100"></e-grid-column>
                                        <e-grid-column headerText="Graph" template="#columnTemplate1" textAlign="@Syncfusion.EJ2.Grids.TextAlign.Center" width="100"></e-grid-column>

                                    </e-grid-columns>
                                </ejs-grid>
                            </div>
                        </div>
                    </div>
                </div>
// and more here... //

 function loadComplete(args) {
            setTimeout(function() {
                    for (var i = 1; i < @Model.Measures.Max(x => x.Id) + 1; i++) {
                        line = new ej.charts.Sparkline({
                            height: '50px',
                            width: '150px',
                            lineWidth: 2.5,
                            valueType: 'Numeric',
                            fill: getSparkColor(i),
                            dataSource: getSparkData('line', i)
                        });
                        line.appendTo('#spkline' + i);
                    }
                },
                1000)
        }

        var lineData = [
            [0, 6, 4, 1, 3, 2, 5],
           ..
           .. 
           ..
            [1, 3, 4, 2, 5, 0, 6]
        ];

        function getSparkColor(count) {
            //var returnValue = String(@ViewBag.Colors[count]);
            //return returnValue;
            return '#3D78EF';
        }

        function getSparkData(type, count) {
            if (type === 'line') {
                return lineData[count];
            }
        }

So....

Is it possible to have getSparkColor and getSparkData to access the ViewBag? It tells me the variable doesn't exist in the context when I attempt to access the [count] in the ViewBag for getSparkColor, had similar issues referencing the 'count' item in getSparkData. 

I essentially want the Color of each sparkline to be different and I obviously need to set each data specifically.

Thanks.



Baby Palanidurai [Syncfusion]
Replied On July 10, 2019 10:22 AM UTC

Hi Mark, 

Greetings from Syncfusion. 

We have analyzed your query and we have prepared a simple sample based on your requirement. In that sample, we are getting the data source for sparkline from the ViewBag and also get the fill color for sparkline charts.  
Please find the below code snippet to achieve this requirement, 
List<DataSource> data1 = new List<DataSource>(); 
data1.Add(new DataSource() { x = 0, y1 = 0, y2 = 1 }); 
// Add more data 
ViewBag.data = data1; 

ViewBag.Colors = new string[] { "#3D78EF", "green" }; 
 
 
 

function loadComplete(args) { 
            setTimeout(function() { 
                    for (var i = 1; i < 3; i++) { 
                        line = new ej.charts.Sparkline({ 
                            height: '50px', 
                            width: '150px', 
                            valueType: 'Numeric', 
                            xName: 'x', 
                            yName: 'y' + i, 
                            fill: getSparkColor(i), 
                            dataSource: @Html.Raw(Json.Serialize(@ViewBag.data)) 
                        }); 
                        line.appendTo('#spkline' + i); 
                    } 
                }, 
                1000) 
        } 
 
function getSparkColor(count) { 
   var colors =   @Html.Raw(Json.Serialize(@ViewBag.Colors)); 
   return colors[count - 1]; 
} 



Kindly revert us, if you have any concerns. 

Regards, 
Baby. 


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.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

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

Live Chat Icon For mobile
Live Chat Icon