parshu shiral
parshu shiral

Reputation: 41

Changing chart theme dynamically in zingchart is not working

I am trying to change the chart theme dynamically using this code:

changeTheme(e) {
        this.previewService.chartConfig.theme = this.themes[e.target.attributes.value.value];
      }

This is the way I am changing the theme dynamically, it got reflected in json source of chart but it is not updating the actual theme. Could you show me a working example for same? Please check this fiddle. https://jsfiddle.net/pshiral/30955m70/7/

Upvotes: 4

Views: 714

Answers (3)

nardecky
nardecky

Reputation: 2631

Full disclosure, I'm a member of the ZingChart team.

I have updated our Angular Directive to watch for changes on the zcRender object. It will take care of chart cleanup and re-rendering the chart for you. The latest version 1.2.1 can be downloaded from github or npm. I put together a small demo in a plnkr demonstrating the changes.

                // If defaults or theme has changed. 
            // Destroy and re-render chart
            $scope.$watch('zcRender', function(newValue, oldValue, scope) {
              if(initializing.render){
                  initializing.render = !initializing.render;
                  return;
              }
              // console.log('---newValue',newValue)
              // console.log('---oldValue',oldValue)
              // console.log('---scope',scope)

              zingchart.exec(scope.id, 'destroy');
              scope.zcRender = newValue;
              scope.renderChart();
            },true);

            // Break render function out of link so we can consistently
            // call the same rendering algorithm
            $scope.renderChart = function (){
              var id = $element.attr('id');
              //Defaults
              var _json = {
                  data : {
                      type : 'line',
                      series : []
                  },
                  width : 600,
                  height: 400
              };

              //Add render object.
              if($scope.zcRender){
                  mergeObject($scope.zcRender, _json);
              }

              //Add JSON object
              if($scope.zcJson){
                  mergeObject($scope.zcJson, _json.data);
              }

              //Add Values
              if($scope.zcValues){
                injectValues($scope.zcValues, _json.data);
              }

              //Add other properties
              _json.data.type = ($attrs.zcType) ? $attrs.zcType : _json.data.type;
              _json.height = ($attrs.zcHeight) ? $attrs.zcHeight : _json.height;
              _json.width = ($attrs.zcWidth) ? $attrs.zcWidth : _json.width;
              _json.id = id;

              //Set the box-model of the container element if the height or width are defined as 100%.
              if(_json.width === "100%" && !$element.css('width')){
                  $element.css('width', '100%');
              }
              if(_json.height === "100%" && !$element.css('height')){
                  $element.css('height', '100%');
              }
              zingchart.render(_json);
          }

Upvotes: 2

Umesh
Umesh

Reputation: 13

you need to use defaults instead of theme : please check attached fiddle , let me know is it the same thing you are looking?

zingchart.render({ id : 'myChart', data : $scope.data, height : 300, width : 500, defaults: $scope.myTheme });

https://jsfiddle.net/omix3379/4k0v06cL/

Thanks.

Upvotes: 0

nardecky
nardecky

Reputation: 2631

Full disclosure, I'm a member of the ZingChart team.

Themes work at the highest level in our library. We build pieces of the chart based on attributes in the theme working our way in a top to bottom fashion. Because of this, you can only set and update a theme at the render level. This means you will have to destroy your chart and re-render it to changes themes. Check out the following:

var myConfig = {
 	type: "bar", 
	series : [
		{
			values : [35,42,67,89,25,34,67,85]
		}
	]
};

zingchart.render({ 
	id : 'myChart', 
	data : myConfig, 
	height: 400, 
	width: 600 
});

function changeTheme(e) {
  zingchart.exec('myChart','destroy');
  zingchart.render({ 
  	id : 'myChart', 
  	data : myConfig, 
  	height : 400, 
  	width : 600,
  	theme: this.id
  });
}
document.getElementById('light').addEventListener('click',changeTheme);
document.getElementById('dark').addEventListener('click',changeTheme);
<!DOCTYPE html>
<html>
	<head>
		<script src= "https://cdn.zingchart.com/zingchart.min.js"></script>
		<script> zingchart.MODULESDIR = "https://cdn.zingchart.com/modules/";</script>
	</head>
	<body>
		<div id='myChart'></div>
		<button id='light'>Light Theme</button>
		<button id='dark'>Dark Theme</button>
	</body>
</html>

Upvotes: 5

Related Questions