[CLOSED] Build Line Char from MVC Controller

  1. #1

    [CLOSED] Build Line Char from MVC Controller

    Hi Ext.Net Team,

    Below is my test code:

    //Model
    public class TestClass
        {
            public double? MeanEstimatedPrice { get; set; }
            public double? MaxEstimatedPrice { get; set; }
            public double? MinEstimatedPrice { get; set; }
            public string CountryName { get; set; }
     
            public static List<TestClass> GetRecords()
            {
                List<TestClass> list = new List<TestClass>();
                list.Add(new TestClass() { MeanEstimatedPrice = 12, MaxEstimatedPrice = 12, MinEstimatedPrice = 10, CountryName = "Albania" });
                list.Add(new TestClass() { MeanEstimatedPrice = 12, MaxEstimatedPrice = 12, MinEstimatedPrice = 10, CountryName = "Lativia" });
                list.Add(new TestClass() { MeanEstimatedPrice = 10, MaxEstimatedPrice = 12, MinEstimatedPrice = 10, CountryName = "France" });
                list.Add(new TestClass() { MeanEstimatedPrice = 16, MaxEstimatedPrice = 12, MinEstimatedPrice = 10, CountryName = "Belgium" });     
                return list;
            }
     
        }
    //View
    @functions
    {
        string currDesc = string.Empty;
        bool isAllCombined = false;
        string CategoryTitle = string.Empty;
        bool IsLocalCurrency = false;
        string NumericAxisTitle = string.Empty;
        double minValue;
        bool isMonteCarlo = false;
      
      
      
        private Store CreateStore(bool isMonteCarlo)
        {
            Store store = new Store();
            store.ID = "StoreGSnapShot1";
            var model = new Model();
             
            model.Fields.Add(new ModelField("MeanEstimatedPrice", ModelFieldType.String));
            model.Fields.Add(new ModelField("CountryName", ModelFieldType.String));
            model.Fields.Add(new ModelField("PACK_CURRENCY_DESC", ModelFieldType.String));
             
            store.Model.Add(model);
      
            return store;
        }
      
        private void Initchart(Container pnlGrafico)
        {
            var result = TestClass.GetRecords();
      
      
            Ext.Net.Chart chtGrafico = new Ext.Net.Chart()
            {
                ID = "SnapShotChart",
                Animate = true,
                Height = 300,
                Shadow = true,
            };
              
            CategoryTitle = "Countries";
      
            chtGrafico.LegendConfig = new ChartLegend() { Position = LegendPosition.Bottom };
      
            var store = CreateStore(isMonteCarlo);
            store.DataSource = result;
            chtGrafico.Store.Add(store);
      
      
            minValue = result.Min(x => x.MeanEstimatedPrice.Value);
            minValue = Math.Round(minValue, 2);
            var maxValue = result.Max(x => x.MeanEstimatedPrice.Value);
      
            //if (minValue > 0.1)
            minValue = 0;
      
            string handlerTips = "";
      
            handlerTips = " var stringValue = '" + currDesc + " '+ Ext.util.Format.number(value, '0.0000'); return stringValue;";
      
            AxisLabel l = new AxisLabel();
            l.Renderer.Handler = handlerTips;
            List<string> listName = new List<string>();
      
            NumericAxis numericAxis = new NumericAxis()
                {
                    Fields = new[] { "MeanEstimatedPrice" },
                    Grid = true,
                    Title = "Price per UOM",
                    Minimum = minValue,
                    Label = l
                };
     
                if (maxValue < 0.1)
                    numericAxis.Maximum = maxValue;
     
                chtGrafico.Axes.Add(numericAxis);
      
            CategoryAxis categoryAxis = new CategoryAxis()
            {
                Fields = new string[] { "CountryName" },
                Title = CategoryTitle,
                Position = Position.Bottom
            };
      
            chtGrafico.Axes.Add(categoryAxis);
      
            var series = CreateChartSeries1(result, isMonteCarlo, listName, isAllCombined);
      
            foreach (var ser in series)
            {
                chtGrafico.Series.Add(ser);
            }
      
          //Add Chart Title Label as a Sprite not working
           var lblChartTitle= new Sprite {Type = SpriteType.Text, Text = "My Custom Text", X = 100, Y = 200, Fill = "#000", SpriteID = "MyCustomText" };
            chtGrafico.Add(lblChartTitle);
      
            //Add Chart
            pnlGrafico.Items.Add(chtGrafico);
        }
      
        private SeriesCollection CreateChartSeries1(List<TestClass> list, bool isMonteCarlo, List<String> llist, bool isAllCombined)
        {
            SeriesCollection absSeries = new SeriesCollection();
            absSeries = new SeriesCollection();
            string handler = string.Empty;
     
     
            var countryList = list.Select(x => x.CountryName).ToList();
     
            var colors = new string
    [list.Count];
            KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
            for (int i = 0; i < list.Count; i++)
            {
                if (i <= 143)
                {
                    KnownColor randomColorName = names[30 + i];
                    colors[i] = Color.FromKnownColor(randomColorName).Name;
                }
                else
                {
                    KnownColor randomColorName = names[i];
                    colors[i] = Color.FromKnownColor(randomColorName).Name;
                }
            }
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            var colorarr = js.Serialize(colors);
     
           
            absSeries.Add(
                        Html.X().ColumnSeries()
                                .Axis(Position.Left)
                                .XField("CountryName")
                                .YField("MeanEstimatedPrice")
                                .Titles(countryList.ToArray())
                                .Highlight(true)
                                .Tips(Html.X().ChartTip()
                                    .TrackMouse(true)
                                    .Width(250)
                                    .Height(28)                               
                                )
                                .Renderer(r => r.Handler = @"
                                    var c= new Array();
                                    var colorsArr =" + colorarr + @";                                
                                        attributes.fill = colorsArr[index % colorsArr.length];
                                        return attributes;")
                        );
             
            return absSeries;
      
        }
    }
    <div>
        @(Html.X().Container()
                    .ID("container_SnapShot")
                    .Height(550)
                    .DefaultAnchor("100%")
                    .Layout(LayoutType.Fit)
                    .Control(item => this.Initchart(item))
             )
      
        @( Html.X().Container().ID("NodataContainer").Items())
        @* //X.Label(Model.ChartTitle).ID("lblSnapShotNoData").StyleSpec("font: bold 18px Arial;").Hidden(true).Text("No data found")))*@
    </div>
    As you can see in the above code, the Initchart method is in the same .cshtml page.
    How can i move this method in the MVC controller i.e. in a .CS file. ?

    Thanks in Advance
    Last edited by Daniil; Nov 29, 2013 at 2:22 PM. Reason: [CLOSED]
  2. #2
    Hi @PriceRightHTML5team,

    Well, there is no a possibility to move it from View to Controller without re-designing. Actually, your current design looks good. Please clarify what is the reason to change it?

    You could move the creation of the Chart to a partial view and wire it up to the container_SnapShot using, for example, ItemsFromAction.
    http://mvc.ext.net/#/Items/Action/

    Though, the code to create the Chart will be in .cshtml still.

    If you do really need to create a Chart in a controller, you could use a Container's Loader. Though, a Chart will be loaded by means of a separate AJAX request.

Similar Threads

  1. Replies: 2
    Last Post: Nov 07, 2013, 1:03 PM
  2. [CLOSED] Can not build Trunk with .NET35 build configuration
    By jchau in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Dec 24, 2012, 2:20 PM
  3. input text mask with L literal char
    By Wirianto Widjaya in forum 1.x Help
    Replies: 4
    Last Post: Mar 30, 2012, 6:35 AM
  4. Allow '+' char in NumberField
    By wdk in forum 1.x Help
    Replies: 5
    Last Post: Apr 27, 2011, 5:39 AM
  5. [CLOSED] Special char problem
    By majestic in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: Jul 26, 2010, 11:32 AM

Tags for this Thread

Posting Permissions