Dynamically changing chart theme in zingchart not working

I am trying to modify a dynamic chart with this code:

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

This is how I dynamically change the topic, it is reflected in the json source of the diagram, but does not update the current topic. Could you show me a working example? Please check this fiddle. https://jsfiddle.net/pshiral/30955m70/7/

+4
source share
3 answers

Full disclosure, I am a member of the ZingChart team.

. , , - . - . , . :

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>
Hide result
+5

, ZingChart.

Angular, zcRender. . 1.2.1 github npm. plnkr, .

                // 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);
          }
+2

you need to use the default values ​​instead of the theme: please attach a violin, let me know is this the same thing you are looking for?

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

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

Thank.

0
source

Source: https://habr.com/ru/post/1652488/


All Articles