Dynamically change chart type and keeping selected series

Jun 14, 2012 at 12:16 PM
Edited Jun 14, 2012 at 12:16 PM

Hi.

I have a chart with different series, (created users, deleted users, active users) over a given period of time.

When the chart is first loaded, only the created users are shown, (the other series have a visible = false).

Here is the code that initially draws feeds the chart with data.

        protected void Button_UserProfileClick(object sender, EventArgs e)
        {
            DateTime? startDate = (BasicDatePicker_StartDate.IsNull ? null : (DateTime?)BasicDatePicker_StartDate.SelectedDate.Date);
            DateTime? endDate = (BasicDatePicker_EndDate.IsNull ? null : (DateTime?)BasicDatePicker_EndDate.SelectedDate.Date);

            if(!startDate.HasValue)
                Chart_Created.Visible = false;
            else
            {
                if(string.IsNullOrEmpty(DropDownList_ChartType.SelectedValue))
                    SetDDLChartTypes();

                Chart_Created.YAxis.Clear();
                Chart_Created.XAxis.Clear();
                Chart_Created.DataSource = null;
                Chart_Created.Series.Clear();
                Chart_Created.Visible = true;
                Chart_Created.Title = new Title("UserProfiles");

                //Get data
                List<ResultClass> resultClassList = UserProfileCreateAndDeleteStatisticsReport.Instance.GetCreatedDeletedUserProfilesFromPeriod(GetCountryId(), GetDefaultPortalSetupId(), GetCustomerId(),
                      startDate.Value.Date, DateTime.Now, false, TimeIntervalEnum.Month);

                List<ResultClass> resultClassListDeleted = UserProfileCreateAndDeleteStatisticsReport.Instance.GetCreatedDeletedUserProfilesFromPeriod(GetCountryId(), GetDefaultPortalSetupId(), GetCustomerId(),
                    startDate.Value.Date, endDate.HasValue ? endDate.Value.Date.AddDays(1) : DateTime.Now, true, TimeIntervalEnum.Month);

                List<ResultClass> resultClassListTotal = UserProfileCreateAndDeleteStatisticsReport.Instance.GetTotalUserProfilesFromPeriod(GetCountryId(), GetDefaultPortalSetupId(), GetCustomerId(),
                    startDate.Value.Date, endDate.HasValue ? endDate.Value.Date.AddDays(1) : DateTime.Now, TimeIntervalEnum.Month);

                List<ResultClass> resultClassListActivated = UserProfileActivatedStatisticsReport.Instance.GetActivatedUserProfilesFromPeriod(GetCountryId(), GetDefaultPortalSetupId(), GetCustomerId(),
                    startDate.Value.Date, endDate.HasValue ? endDate.Value.Date.AddDays(1) : DateTime.Now, TimeIntervalEnum.Month);

                //Makes sure that all the dates in the period with data is shown
                List<DateTime> dateTimeList = new List<DateTime>();
                dateTimeList.AddRange(resultClassList.Select(a => a.dt).ToList());
                dateTimeList.AddRange(resultClassListDeleted.Select(a => a.dt).ToList());
                dateTimeList.AddRange(resultClassListTotal.Select(a => a.dt).ToList());
                dateTimeList.AddRange(resultClassListActivated.Select(a => a.dt).ToList());

                Dictionary<DateTime, DateTime> dateTimeDictionary = new Dictionary<DateTime, DateTime>();
                foreach(DateTime dateTime in dateTimeList)
                {
                    if(dateTimeDictionary.ContainsKey(dateTime))
                        continue;
                    else
                        dateTimeDictionary.Add(dateTime, dateTime);
                }
                RenderType rednerType = (RenderType)Enum.Parse(typeof(RenderType), DropDownList_ChartType.SelectedValue);

                Chart_Created.YAxis.Add(new YAxisItem { title = new Title("UserProfiles") });
                Chart_Created.XAxis.Add(new XAxisItem { categories = dateTimeDictionary.Values.ToList().Select(a => a.ToShortDateString()).ToArray() });
     

                Collection<Serie> seriesCollection = new Collection<Serie>();
                seriesCollection.Add(new Serie { data = resultClassList.Select(a => (object)a.count).ToArray(), name = "Created", type = rednerType });
                seriesCollection.Add(new Serie { data = resultClassListActivated.Select(a => (object)a.count).ToArray(), name = "Activated", visible = false, type = rednerType });
                seriesCollection.Add(new Serie { data = resultClassListDeleted.Select(a => (object)a.count).ToArray(), name = "Deleted", visible = false, type = rednerType });
                seriesCollection.Add(new Serie { data = resultClassListTotal.Select(a => (object)a.count).ToArray(), name = "Total", visible = false, type = rednerType });

                Chart_Created.DataSource = seriesCollection;
                Chart_Created.DataBind();
            }
And here is the code that is fired when the index of the dropdownlist with the chart types changes
        protected void DDLChartTypeSelectedIndexCahged(object sender, EventArgs e)
        {

            RenderType rednerType = (RenderType)Enum.Parse(typeof(RenderType), DropDownList_ChartType.SelectedValue);

            foreach(Serie item in Chart_Created.Series)
            {
                item.type = rednerType;
            }

            Chart_Created.Render();
        }

 

This results in the chart being rendered in the new type, however. all the series that's been set to visible (except created users) is set to visible false, and if created users is set to visible false it changes to visible true.

 

What I want is that the selected series persist through the change of the chart type, any idea on how to do this?