<?php 

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class X_Dynamic_Chart extends \Bricks\Element {

  // Element properties
  public $category     = 'extras';
  public $name         = 'xdynamicchart'; 
  public $icon         = 'ti-bar-chart';
  public $css_selector = '';
  public $scripts      = ['xDynamicChart'];
  public $loop_index = 0;
  public $flat_datapoints = [];
  private static $script_localized = false;

  // Methods: Builder-specific
  public function get_label() {
	return esc_html__( 'Dynamic Chart', 'bricksextras' );
  }

  public function get_keywords() {
    return [ 'dynamic chart', 'chart' ];
  }

  public function set_control_groups() {

	$this->control_groups['table_content_group'] = [
		'title' => esc_html__( 'Data', 'bricks' ),
		'tab' => 'content',
	];

	$this->control_groups['chart_type_group'] = [
		'title' => esc_html__( 'Chart style', 'bricksextras' ),
		'tab' => 'content',
	];

	$this->control_groups['axis_group'] = [
		'title' => esc_html__( 'Axes', 'bricksextras' ),
		'tab' => 'content',
		'required' => ['chart_type', '!=', 'doughnut']
	];

	$this->control_groups['tooltip_group'] = [
		'title' => esc_html__( 'Tooltips', 'bricksextras' ),
		'tab' => 'content',
	];

	$this->control_groups['legend_group'] = [
		'title' => esc_html__( 'Legends', 'bricksextras' ),
		'tab' => 'content',
	];

	$this->control_groups['data_labels_group'] = [
		'title' => esc_html__( 'Data labels', 'bricksextras' ),
		'tab' => 'content',
	];

	$this->control_groups['spacing_group'] = [
		'title' => esc_html__( 'Spacing', 'bricks' ),
		'tab' => 'content',
	];
	
	$this->control_groups['dataset_colors_group'] = [
		'title' => esc_html__( 'Dataset colors', 'bricksextras' ),
		'tab' => 'content',
	];

	

  }


  public function set_controls() {

		$this->controls['chartWidth'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Width', 'bricks' ),
			'inline' => true,
			'type' => 'number',
			'units'    => true,
			'css' => [
				[
					'selector' => '',  
					'property' => 'width',
				],
			],
			'placeholder' => '800px',
		];

		$this->controls['chartAspectRatio'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Aspect ratio', 'bricks' ),
			'inline' => true,
			'type' => 'text',
			'hasDynamicData' => false,
			'units'    => false,
		];

		$this->controls['ariaLabel'] = [
			'tab' => 'content',
			'label' => 'aria-label',
			'type' => 'text',
			'placeholder' => esc_html__( 'Chart', 'bricksextras' ),
			'inline'  => true,
		];

		$this->controls['loop_sep'] = [
			'tab'   => 'content',
			'type'  => 'separator',
			//'label' => esc_html__( 'Dynamic data to populate rows', 'bricksextras' ),
			//'description' => esc_html__( 'Use query loop if needing to use multiple datasets', 'bricksextras' ),
		];

		$this->controls = array_replace_recursive( $this->controls, $this->get_loop_builder_controls() );

		$this->controls['queryLoopDatapoints'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Use query loop for..', 'bricksextras' ),
			'type' => 'select',
			'inline' => true,
			'options' => [ 
				'dataSets' =>  esc_html__( 'Datasets', 'bricksextras' ),
				'dataPoints' =>  esc_html__( 'Data points', 'bricksextras' ), 
				'array' =>  'JSON' . ' (' . esc_html__( 'Query Array', 'bricks' ) . ')',  
			],
			'placeholder' => esc_html__( 'Datasets', 'bricksextras' ),
			'required' => [ 'hasLoop', '!=', false ]
			
		];

		$this->controls['queryLoopDatapoints_Label'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Label', 'bricks' ),
			'group'			=> 'table_content_group',
			'type' => 'text',
			'required' => ['queryLoopDatapoints', '=', 'dataPoints']
		];

		$this->controls['queryLoopDatapoints_Value'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Value', 'bricks' ),
			'group'			=> 'table_content_group',
			'type' => 'text',
			'required' => ['queryLoopDatapoints', '=', 'dataPoints']
		];

		$this->controls['flatArray_DatasetIdentifier'] = [ 
			'tab' => 'content',
			'label' => esc_html__( 'Dataset', 'bricksextras' ),
			'group'			=> 'table_content_group',
			'info' => 'Use {query_array} to identify which dataset (leave blank if only one)',
			'type' => 'text',
			'inline'	=> true,
			'placeholder' => '{query_array @key:\'dataset\'}',
			'required' => ['queryLoopDatapoints', '=', 'array']
		];

		$this->controls['flatArray_XAxisLabel'] = [
			'tab' => 'content', 
			'label' => esc_html__( 'Data point', 'bricksextras' ),
			'group'			=> 'table_content_group',
			'type' => 'text',
			'placeholder' => '{query_array @key:\'datapoint\'}',
			'info' => 'Use {query_array} to identify the datapoint',
			'inline'	=> true,
			'required' => ['queryLoopDatapoints', '=', 'array']
		];

		$this->controls['flatArray_Value'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Value', 'bricks' ),
			'group'			=> 'table_content_group',
			'type' => 'text',
			'placeholder' => '{query_array @key:\'value\'}',
			'info' => 'Use {query_array} for the numeric value',
			'inline'	=> true,
			'required' => ['queryLoopDatapoints', '=', 'array']
		];

		$this->controls['content_items'] = [
			'tab'           => 'content',
			'type'          => 'repeater',
			'group'			=> 'table_content_group',
			'required' => [['queryLoopDatapoints', '!=', 'dataPoints'], ['queryLoopDatapoints', '!=', 'array']],
			'label'         => esc_html__( 'Data points', 'bricksextras' ),
			'placeholder'   => esc_html__( 'Data point', 'bricksextras' ),
			'titleProperty' => 'title',
			'fields'        => [
				'title'       => [
					'type'    => 'text',
					'label'   => esc_html__( 'Label', 'bricks' ),
					//'inline'  => true,
				],
				'data'       => [
					'type'    => 'text',
					'label'   => esc_html__( 'Value', 'bricks' ),
					//'inline'  => true,
				],
				'color'       => [
					'type'    => 'color',
					'label'   => esc_html__( 'Data point color', 'bricksextras' ),
					'description' => '(' . esc_html__( 'If using only one dataset', 'bricksextras' ) . ')'
				],
			],
			'default'     => [
				[
					'title'      => esc_html__( 'January', 'bricks' ),
					'data'      => '20',
					'color'	    => [
						'hex' => '#1da69a',
					]
				],
				[
					'title'      => esc_html__( 'February', 'bricks' ),
					'data'      => '70',
					'color'	    => [
						'hex' => '#072027',
					]
				],
			]
		];

		$this->controls['xAxisUnits'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Units for values', 'bricksextras' ),
			'type' => 'text',
			'info' => 'Units to show next to numbers. eg % or years',
			'group'	=> 'table_content_group',
			'required' => ['xAxisDisplay', '!=', 'false'],
			'inline'      => true,
		  ];

		 $this->controls['xAxisUnitPosition'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Position', 'bricks' ),
			'type' => 'select',
			'options' => [
				'after' => esc_html__( 'After', 'bricks' ),
				'before' => esc_html__( 'Before', 'bricks' ),
			  ],
			'group'	=> 'table_content_group',
			'placeholder' => esc_html__( 'After', 'bricks' ),
			'required' => [
				['xAxisDisplay', '!=', 'false'],
				['xAxisUnits', '!=', '']
			],
			'inline'      => true,
		  ];  

		$this->controls['dataSetColorsStart'] = [
			'tab'   => 'content',
			'type'  => 'separator',
			'group'	=> 'dataset_colors_group',
			'description' => esc_html__( 'Use only if using multiple datasets. Set a colour for each set (either manually or using dynamic data).', 'bricksextras' ),
		];

		$this->controls['maybeDynamicColors'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Set dataset colors', 'bricksextras' ),
			'type' => 'select',
			'group'	=> 'dataset_colors_group',
			'options' => [
			  'manually' => esc_html__( 'Manually', 'bricksextras' ),
			  'dynamic' => esc_html__( 'Use dynamic data', 'bricksextras' ),
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Manually', 'bricksextras' ),
		  ];


		$this->controls['datasetColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Dataset color', 'bricksextras' ),
			'group'	=> 'dataset_colors_group',
			'type' => 'text',
			'inline' => true,
			'required' => ['maybeDynamicColors', '=', 'dynamic'],
		  ];

		$this->controls['datasetItems'] = [
			'tab'           => 'content',
			'type'          => 'repeater',
			//'checkLoop'   => true,
			'group'			=> 'dataset_colors_group',
			'label'         => esc_html__( 'Dataset colors', 'bricksextras' ),
			//'description'         => esc_html__( 'If using multiple datasets, set the styles individually', 'bricksextras' ),
			'titleProperty' => 'type',
			'fields'        => [
				'color'       => [
					'type'    => 'color',
					'label'   => esc_html__( 'Color', 'bricks' ),
					//'inline'  => true,
				],
			],
			'required' => ['maybeDynamicColors', '!=', 'dynamic'],
		];

		$this->controls['chartTypography'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Typography', 'bricks' ),
			'type' => 'typography',
			'group'	=> 'chart_type_group',
			'rerender' => true,
			'exclude' => [ 
				'color',
				'text-align',
				'text-wrap',
				'text-decoration',
				'white-space',
				'font-variation-settings',
				'text-shadow',
				'text-transform',
				'letter-spacing',
			],
		];

		$this->controls['chartDirection'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Direction', 'bricks' ),
			'type' => 'select',
			'group'	=> 'chart_type_group',
			'options' => [
			  'horizontal' => esc_html__( 'Horizontal', 'bricks' ),
			  'vertical' => esc_html__( 'Vertical', 'bricks' ),
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Vertical', 'bricks' ),
			'required' => ['chart_type', '!=', 'doughnut'],
		  ];

		
		$this->controls['chart_type'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Type', 'bricks' ),
			'type' => 'select',
			'group'	=> 'chart_type_group',
			'options' => [
			  'bar' => esc_html__( 'Bar', 'bricks' ),
			  'line' => esc_html__( 'Line', 'bricksextras' ),
			  'doughnut' => esc_html__( 'Pie', 'bricksextras' ) . ' / ' . esc_html__( 'Doughnut', 'bricksextras' ),
			  
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Bar', 'bricks' ),
		  ];

		  $this->controls['stacked'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Chart stacking', 'bricksextras' ),
			'type' => 'select',
			'group'	=> 'chart_type_group',
			'options' => [
			  'disable' => esc_html__( 'Disable', 'bricks' ),
			  'stack' => esc_html__( 'Stack on top', 'bricksextras' ),
			  'overlap' => esc_html__( 'Overlap', 'bricksextras' ),
			],
			'inline' => true,
			'placeholder' => esc_html__( 'Disable', 'bricks' ),
			'required' => ['chart_type', '!=', 'doughnut'],
		  ];

		  $this->controls['reverseDatasets'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Reverse dataset order', 'bricksextras' ),
			'type' => 'checkbox',
			'group'	=> 'chart_type_group',
			'inline' => true,
			'required' => [
				['chart_type', '!=', 'doughnut'],
			],
		  ];

		  $this->controls['lineSep'] = [
			'tab' => 'content',
			'type' => 'separator',
			'label' => esc_html__( 'Line', 'bricksextras' ),
			'group'	=> 'chart_type_group',
			'required' => ['chart_type', '=', 'line'],
		  ];

		  

		  $this->controls['lineColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Color', 'bricks' ),
			'type' => 'color',
			'group'	=> 'chart_type_group',
			'required' => ['chart_type', '=', ['line','doughnut']],
		  ];

		  $this->controls['tension'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Tension', 'bricksextras' ),
			'type' => 'slider',
			'group'	=> 'chart_type_group',
			'units' => false,
			'min' => 0,
			'max' => 1,
			'step' => .01,
			'placeholder' => '0',
			'required' => ['chart_type', '=', 'line'],
		  ];

		  $this->controls['pieBorderWidth'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Line width', 'bricksextras' ),
			'type' => 'number',
			'group'	=> 'chart_type_group',
			'units' => false,
			'min' => 0,
			'max' => 10,
			'step' => 1,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
			'required' => ['chart_type', '=', ['doughnut']],
		  ];

		  $this->controls['pieCutOut'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Center cutout', 'bricksextras' ) . ' (%)',
			'type' => 'number',
			'group'	=> 'chart_type_group',
			'units' => false,
			'min' => 0,
			'max' => 100,
			'step' => 1,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
			'required' => ['chart_type', '=', ['doughnut']],
		  ];

		  $this->controls['lineWidth'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Line width', 'bricksextras' ),
			'type' => 'number',
			'group'	=> 'chart_type_group',
			'units' => false,
			'min' => 0,
			'max' => 10,
			'step' => 1,
			'small' => true,
			'placeholder' => esc_html__( '2', 'bricks' ),
			'required' => ['chart_type', '=', ['line']],
		  ];

		  $this->controls['pointSep'] = [
			'tab' => 'content',
			'type' => 'separator',
			'label' => esc_html__( 'Point', 'bricksextras' ),
			'group'	=> 'chart_type_group',
			'required' => ['chart_type', '=', 'line'],
		  ];

		  $this->controls['linePointRadius'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Size', 'bricks' ),
			'group'	=> 'chart_type_group',
			'type' => 'number',
			'unit' => 'px',
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '3', 'bricks' ),
			'required' => ['chart_type', '=', 'line'],
		  ];

		  $this->controls['linePointBorderWidth'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Border width', 'bricks' ),
			'group'	=> 'chart_type_group',
			'type' => 'number',
			'unit' => 'px',
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '1', 'bricks' ),
			'required' => ['chart_type', '=', 'line'],
		  ];

		  $this->controls['barBorderRadius'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Border radius', 'bricksextras' ),
			'group'	=> 'chart_type_group',
			'type' => 'number',
			'unit' => 'px',
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '5', 'bricks' ),
			'required' => ['chart_type', '!=', ['line','doughnut']],
		  ];

		  $this->controls['barBorderColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Border color', 'bricks' ),
			'group'	=> 'chart_type_group',
			'type' => 'color',
			'inline' => true,
			'small' => true,
			'required' => ['chart_type', '!=', ['line','doughnut']],
		  ];

		  $this->controls['barBorderWidth'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Border width', 'bricks' ),
			'group'	=> 'chart_type_group',
			'type' => 'number',
			'unit' => 'px',
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
			'required' => ['chart_type', '!=', ['line','doughnut']],
		  ];

		  $this->controls['rotation'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Rotation', 'bricksextras' ),
			'group'	=> 'chart_type_group',
			'type' => 'number',
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
			'required' => ['chart_type', '=', ['doughnut']],
		  ];

		  $this->controls['circumference'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Circumference', 'bricksextras' ),
			'group'	=> 'chart_type_group',
			'type' => 'number',
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '360', 'bricks' ),
			'required' => ['chart_type', '=', ['doughnut']],
		  ];




		  /* x axis */

		  $this->controls['xAxisStart'] = [
			'tab'   => 'content',
			'group'  => 'axis_group',
			'type'  => 'separator',
			'label' => esc_html__( 'x-axis', 'bricks' ),
		  ];


		  $this->controls['xAxisDisplay'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Enable', 'bricks' ),
			'type' => 'select',
			'group'	=> 'axis_group',
			'options' => [
			  'true' => esc_html__( 'Enable', 'bricks' ),
			  'false' => esc_html__( 'Disable', 'bricks' ),
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Enable', 'bricks' ),
		  ];

		  $this->controls['xAxisTitleSep'] = [
			'tab' => 'content',
			'group'	=> 'axis_group',
			'type'  => 'separator',
			'label' => esc_html__( 'Title', 'bricks' ),
			'required' => ['xAxisDisplay', '!=', 'false'],
		  ];

		  $this->controls['xAxisTitle'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Title', 'bricks' ),
			'group'	=> 'axis_group',
			'type' => 'text',
			'inline' => true,
			'placeholder' => '',
			'required' => ['xAxisDisplay', '!=', 'false'],
		  ];

		  $this->controls['xAxisTitleColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Color', 'bricks' ),
			'type' => 'color',
			'group'	=> 'axis_group',
			'required' => ['xAxisDisplay', '!=', 'false'],
		  ];

          $this->controls['xAxisFont'] = [
            'tab' => 'content',
            'label' => esc_html__( 'Typography', 'bricks' ),
            'type' => 'typography',
            'group' => 'axis_group',
            'required' => ['xAxisDisplay', '!=', 'false'],
            'exclude' => [
                'color',
                'text-align',
                'text-decoration',
                'text-transform',
                'letter-spacing',
                'white-space',
                'text-shadow',
                'text-wrap',
                'font-variation-settings',
            ],
          ];

		  $this->controls['xAxisLabelsSep'] = [
			'tab' => 'content',
			'group'	=> 'axis_group',
			'type'  => 'separator',
			'label' => esc_html__( 'Labels', 'bricks' ),
			'required' => ['xAxisDisplay', '!=', 'false'],
		  ];

		  $this->controls['xAxisTicksColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Color', 'bricks' ),
			'type' => 'color',
			'group'	=> 'axis_group',
			'required' => ['xAxisDisplay', '!=', 'false'],
		  ];

          $this->controls['xAxisTicksFont'] = [
            'tab' => 'content',
            'label' => esc_html__( 'Typography', 'bricks' ),
            'type' => 'typography',
            'group' => 'axis_group',
            'required' => ['xAxisDisplay', '!=', 'false'],
            'exclude' => [
                'color',
                'text-align',
                'text-decoration',
                'text-transform',
                'letter-spacing',
                'white-space',
                'text-shadow',
                'text-wrap',
                'font-variation-settings',
            ],
          ];

		  $this->controls['xGridColor'] = [
			'tab'   => 'content',
			'group'	=> 'axis_group',
			'label' => esc_html__( 'Color', 'bricks' ) . ' (' . esc_html__( 'Grid', 'bricks' ) . ')',
			'type'  => 'color',
			'required' => ['xAxisDisplay', '!=', 'false'],
		  ];

		  $this->controls['xBorderColor'] = [
			'tab'   => 'content',
			'group'	=> 'axis_group',
			'label' => esc_html__( 'Border color', 'bricks' ),
			'type'  => 'color',
			'required' => ['xAxisDisplay', '!=', 'false'],
		  ];

		  

		  /* y axis */

		  $this->controls['yAxisStart'] = [
			'tab'   => 'content',
			'group'  => 'axis_group',
			'type'  => 'separator',
			'label' => esc_html__( 'y-axis', 'bricks' ),
		  ];

		  $this->controls['yAxisDisplay'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Enable', 'bricks' ),
			'type' => 'select',
			'group'	=> 'axis_group',
			'options' => [
			  'true' => esc_html__( 'Enable', 'bricks' ),
			  'false' => esc_html__( 'Disable', 'bricks' ),
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Enable', 'bricks' ),
		  ];

		  $this->controls['yAxisTitleSep'] = [
			'tab' => 'content',
			'group'	=> 'axis_group',
			'type'  => 'separator',
			'label' => esc_html__( 'Title', 'bricks' ),
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];


		  $this->controls['yAxisTitle'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Title', 'bricks' ),
			'group'	=> 'axis_group',
			'type' => 'text',
			'inline' => true,
			'placeholder' => '',
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];

		  $this->controls['yAxisTitleColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Color', 'bricks' ),
			'type' => 'color',
			'group'	=> 'axis_group',
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];

          $this->controls['yAxisFont'] = [
            'tab' => 'content',
            'label' => esc_html__( 'Typography', 'bricks' ),
            'type' => 'typography',
            'group' => 'axis_group',
            'required' => ['yAxisDisplay', '!=', 'false'],
            'exclude' => [
                'color',
                'text-align',
                'text-decoration',
                'text-transform',
                'letter-spacing',
                'white-space',
                'text-shadow',
                'text-wrap',
                'font-variation-settings',
            ],
          ]; 

		  $this->controls['yAxisTicksSep'] = [
			'tab' => 'content',
			'group' => 'axis_group',
			'type' => 'separator',
			'label' => esc_html__( 'Values', 'bricksextras' ),
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];

		  $this->controls['yAxisTicksColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Color', 'bricks' ),
			'type' => 'color',
			'group'	=> 'axis_group',
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];


          $this->controls['yAxisTicksFont'] = [
            'tab' => 'content',
            'label' => esc_html__( 'Typography', 'bricks' ),
            'type' => 'typography',
            'group' => 'axis_group',
            'required' => ['yAxisDisplay', '!=', 'false'],
            'exclude' => [
                'color',
                'text-align',
                'text-decoration',
                'text-transform',
                'letter-spacing',
                'white-space',
                'text-shadow',
                'text-wrap',
                'font-variation-settings',
            ],
          ];

		  $this->controls['yAxisType'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Type', 'bricks' ),
			'type' => 'select',
			'group'	=> 'axis_group',
			'options' => [
			  'linear' => esc_html__( 'Linear', 'bricks' ),
			  'logarithmic' => esc_html__( 'Logarithmic', 'bricksextras' ),
			],
			'inline'      => true,
			//'clearable' => false,
			'placeholder' => esc_html__( 'Linear', 'bricks' ),
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];

		  $this->controls['beginAtZero'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Begin At Zero', 'bricksextras' ),
			'type' => 'select',
			'group'	=> 'axis_group',
			'options' => [
			  'true' => esc_html__( 'Enable', 'bricks' ),
			  'false' => esc_html__( 'Disable', 'bricks' ),
			],
			'inline'      => true,
			'required' => ['yAxisDisplay', '!=', 'false'],
			'placeholder' => esc_html__( 'Enable', 'bricks' ),
		  ];

		  $this->controls['yGridColor'] = [
			'tab'   => 'content',
			'group'	=> 'axis_group',
			'label' => esc_html__( 'Color', 'bricks' ) . ' (' . esc_html__( 'Grid', 'bricks' ) . ')',
			'type'  => 'color',
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];

		  $this->controls['yBorderColor'] = [
			'tab'   => 'content',
			'group'	=> 'axis_group',
			'label' => esc_html__( 'Border color', 'bricks' ),
			'type'  => 'color',
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];

		  
		  $this->controls['suggestedMin'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Suggested Min', 'bricksextras' ),
			'group'	=> 'axis_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];

		  $this->controls['suggestedMax'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Suggested Max', 'bricksextras' ),
			'group'	=> 'axis_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'required' => ['yAxisDisplay', '!=', 'false'],
		  ];

		  
		  /*   tooltips */

		  
		  $this->controls['tooltipDisplay'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Enable', 'bricks' ),
			'type' => 'select',
			'group'	=> 'tooltip_group',
			'options' => [
			  'true' => esc_html__( 'Enable', 'bricks' ),
			  'false' => esc_html__( 'Disable', 'bricks' ),
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Disable', 'bricks' ),
		  ];

		  $this->controls['events'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Events', 'bricks' ),
			'type' => 'select',
			'group'	=> 'tooltip_group',
			'options' => [
			  'click' => esc_html__( 'Click', 'bricks' ),
			  'hover' => esc_html__( 'Hover', 'bricks' ) . ' / ' . esc_html__( 'Click', 'bricks' ),
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Click', 'bricks' ),
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

		   $this->controls['tooltipIntersect'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Accuracy', 'bricksextras' ),
			'type' => 'select',
			'group'	=> 'tooltip_group',
			'options' => [
			  'default' => esc_html__( 'Default', 'bricks' ),
			  'true' => esc_html__( 'Open on direct hover only', 'bricksextras' ),
			  'false' => esc_html__( 'Open on hover nearby', 'bricksextras' ),
			],
			'inline'      => true,
			'placeholder' => esc_html__( 'Default', 'bricks' ),
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

		  $this->controls['tooltipInteractionMode'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Content', 'bricks' ),
			'type' => 'select',
			'group'	=> 'tooltip_group',
			'options' => [
			  'default' => esc_html__( 'Default', 'bricks' ),
			  'index' => esc_html__( 'Values from all datasets in single tooltip', 'bricksextras' ),
			  'nearest' => esc_html__( 'Values from nearest dataset in separate tooltips', 'bricksextras' ),
			],
			'inline'      => true,
			'placeholder' => esc_html__( 'Default', 'bricks' ),
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

		 

		  $this->controls['tooltipColor'] = [
			'tab'   => 'content',
			'group'	=> 'tooltip_group',
			'label' => esc_html__( 'Text color', 'bricks' ),
			'type'  => 'color',
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

		  $this->controls['tooltipBorderColor'] = [
			'tab'   => 'content',
			'group'	=> 'tooltip_group',
			'label' => esc_html__( 'Border color', 'bricks' ),
			'type'  => 'color',
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

		  $this->controls['tooltipBorderWidth'] = [
			'tab'   => 'content',
			'group'	=> 'tooltip_group',
			'label' => esc_html__( 'Border width', 'bricks' ),
			'type'  => 'number',
			'placeholder' => esc_html__( '1', 'bricks' ),
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

		  $this->controls['tooltipCaretSize'] = [
			'tab'   => 'content',
			'group'	=> 'tooltip_group',
			'label' => esc_html__( 'Size', 'bricks' ) . ' (' . esc_html__( 'Caret', 'bricks' ) . ')',
			'type'  => 'number',
			'placeholder' => esc_html__( '5', 'bricks' ),
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

		  $this->controls['tooltipBackground'] = [
			'tab'   => 'content',
			'group'	=> 'tooltip_group',
			'label' => esc_html__( 'Background color', 'bricks' ),
			'type'  => 'color',
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

		  $this->controls['tooltipTitleSep'] = [
			'tab' => 'content',
			'group' => 'tooltip_group',
			'type' => 'separator',
			'label' => esc_html__( 'Title', 'bricks' ),
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

          $this->controls['tooltipTitleFont'] = [
            'tab' => 'content',
            'label' => esc_html__( 'Typography', 'bricks' ),
            'type' => 'typography',
            'group' => 'tooltip_group',
            'exclude' => [
                'color',
                'text-align',
                'text-decoration',
                'text-transform',
                'letter-spacing',
                'white-space',
                'text-shadow',
                'text-wrap',
                'font-variation-settings',
            ],
			'required' => ['tooltipDisplay', '=', 'true'],
          ];

		  $this->controls['tooltipBodySep'] = [
			'tab' => 'content',
			'group' => 'tooltip_group',
			'type' => 'separator',
			'label' => esc_html__( 'Body', 'bricks' ),
			'required' => ['tooltipDisplay', '=', 'true'],
		  ];

          $this->controls['tooltipBodyFont'] = [
            'tab' => 'content',
            'label' => esc_html__( 'Typography', 'bricks' ),
            'type' => 'typography',
            'group' => 'tooltip_group',
            'exclude' => [
                'color',
                'text-align',
                'text-decoration',
                'text-transform',
                'letter-spacing',
                'white-space',
                'text-shadow',
                'text-wrap',
                'font-variation-settings',
            ],
			'required' => ['tooltipDisplay', '=', 'true'],
          ];


		  /*   legend */

		  
		  $this->controls['legendDisplay'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Enable', 'bricks' ),
			'type' => 'select',
			'group'	=> 'legend_group',
			'options' => [
			  'true' => esc_html__( 'Enable', 'bricks' ),
			  'false' => esc_html__( 'Disable', 'bricks' ),
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Disable', 'bricks' ),
		  ];

		  $this->controls['datasetLabel'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Legend', 'bricksextras' ),
			'group'	=> 'legend_group',
			'type' => 'text',
			'inline' => true,
			'placeholder' => '',
			'required' => ['legendDisplay', '=', 'true'],
		  ];

		  $this->controls['legendPosition'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Position', 'bricks' ),
			'type' => 'select',
			'group'	=> 'legend_group',
			'options' => [
			  'top' => esc_html__( 'Top', 'bricks' ),
			  'left' => esc_html__( 'Left', 'bricks' ),
			  'bottom' => esc_html__( 'Bottom', 'bricks' ),
			  'right' => esc_html__( 'Right', 'bricks' ),
			  
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Top', 'bricks' ),
			'required' => ['legendDisplay', '=', 'true'],
		  ];

		  $this->controls['legendAlign'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Align', 'bricks' ),
			'type' => 'select',
			'group'	=> 'legend_group',
			'options' => [
			  'start' => esc_html__( 'Start', 'bricks' ),
			  'center' => esc_html__( 'Center', 'bricks' ),
			  'end' => esc_html__( 'End', 'bricks' ),
			  
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Center', 'bricks' ),
			'required' => ['legendDisplay', '=', 'true'],
		  ];

		  $this->controls['legendColor'] = [
            'tab' => 'content',
            'label' => esc_html__( 'Color', 'bricks' ),
            'type' => 'color',
            'group' => 'legend_group',
			'required' => ['legendDisplay', '=', 'true'],
          ];

          $this->controls['legendFont'] = [
            'tab' => 'content',
            'label' => esc_html__( 'Typography', 'bricks' ),
            'type' => 'typography',
            'group' => 'legend_group',
            'exclude' => [
                'color',
                'text-align',
                'text-decoration',
                'text-transform',
                'letter-spacing',
                'white-space',
                'text-shadow',
                'text-wrap',
                'font-variation-settings',
            ],
			'required' => ['legendDisplay', '=', 'true'],
          ];

		  $this->controls['legendPadding'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Padding', 'bricks' ),
			'group'	=> 'legend_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '10', 'bricks' ),
			'required' => ['legendDisplay', '=', 'true'],
		  ];

		  $this->controls['legendBoxWidth'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Width', 'bricks' ),
			'group'	=> 'legend_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '15', 'bricks' ),
			'required' => ['legendDisplay', '=', 'true'],
		  ];

		  $this->controls['legendBoxHeight'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Height', 'bricks' ),
			'group'	=> 'legend_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '12', 'bricks' ),
			'required' => ['legendDisplay', '=', 'true'],
		  ];


		  /*   data labels */

		  
		  $this->controls['dataLabelsDisplay'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Enable', 'bricks' ),
			'type' => 'select',
			'group'	=> 'data_labels_group',
			'options' => [
			  'true' => esc_html__( 'Enable', 'bricks' ),
			  'false' => esc_html__( 'Disable', 'bricks' ),
			],
			'inline'      => true,
			'clearable' => false,
			'placeholder' => esc_html__( 'Disable', 'bricks' ),
		  ];

		  
		 

		  $this->controls['dataLabelsAnchor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Anchor', 'bricksextras' ),
			'type' => 'select',
			'group'	=> 'data_labels_group',
			'options' => [
			  'start' => esc_html__( 'Start', 'bricks' ),
			  'center' => esc_html__( 'Center', 'bricks' ),
			  'end' => esc_html__( 'End', 'bricks' ),
			],
			'inline'      => true,
			'placeholder' => esc_html__( 'Center', 'bricks' ),
			'required' => ['dataLabelsDisplay', '=', 'true'],
		  ];

		  $this->controls['dataLabelsAlign'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Align', 'bricks' ),
			'type' => 'select',
			'group'	=> 'data_labels_group',
			'options' => [
			  'start' => esc_html__( 'Start', 'bricks' ),
			  'center' => esc_html__( 'Center', 'bricks' ),
			  'end' => esc_html__( 'End', 'bricks' ),
			],
			'inline'      => true,
			'placeholder' => esc_html__( 'Center', 'bricks' ),
			'required' => ['dataLabelsDisplay', '=', 'true'],
		  ];

		  $this->controls['dataLabelsOffset'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Offset', 'bricks' ),
			'group'	=> 'data_labels_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '4', 'bricks' ),
			'required' => [
				['dataLabelsDisplay', '=', 'true'],
				['dataLabelsAlign', '!=', 'center'],
			],
		  ];

		  $this->controls['dataLabelsPadding'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Padding', 'bricks' ),
			'group'	=> 'data_labels_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '6', 'bricks' ),
			'required' => ['dataLabelsDisplay', '=', 'true'],
		  ];


		  $this->controls['dataLabelsColorSeperator'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Styles', 'bricks' ),
			'group'	=> 'data_labels_group',
			'type' => 'separator',
			'required' => ['dataLabelsDisplay', '=', 'true'], 
		  ];

		  $this->controls['dataLabelsColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Text color', 'bricks' ),
			'group'	=> 'data_labels_group',
			'type' => 'color',
			'inline' => true,
			'required' => ['dataLabelsDisplay', '=', 'true'], 
		  ];

		  $this->controls['dataLabelsBackgroundColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Background color', 'bricks' ),
			'info' => esc_html__( 'Leave empty to inherit color', 'bricksextras' ),
			'group'	=> 'data_labels_group',
			'type' => 'color',
			'inline' => true,
			'required' => ['dataLabelsDisplay', '=', 'true'],
		  ];

		$this->controls['dataLabelsBorderColor'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Border color', 'bricks' ),
			'info' => esc_html__( 'Leave empty to inherit color', 'bricksextras' ),
			'group'	=> 'data_labels_group',
			'type' => 'color',
			'inline' => true,
			'required' => ['dataLabelsDisplay', '=', 'true'],
		  ];

		  $this->controls['dataLabelsBorderWidth'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Border width', 'bricks' ),
			'group'	=> 'data_labels_group',
			'type' => 'number',
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
			'required' => ['dataLabelsDisplay', '=', 'true'],
		  ];

		  $this->controls['dataLabelsRotation'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Rotation', 'bricksextras' ),
			'group'	=> 'data_labels_group',
			'type' => 'number',
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
			'required' => ['dataLabelsDisplay', '=', 'true'],
		  ];

		  $this->controls['dataLabelsBorderRadius'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Border radius', 'bricksextras' ),
			'group'	=> 'data_labels_group',
			'type' => 'number',
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
			'required' => ['dataLabelsDisplay', '=', 'true'],
		  ];

		$this->controls['dataLabelsFontSize'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Font size', 'bricks' ),
			'group'	=> 'data_labels_group',
			'type' => 'number',
			'inline' => true,
			'small' => true,
			'required' => ['dataLabelsDisplay', '=', 'true'],
		];

		$this->controls['dataLabelsFontWeight'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Font weight', 'bricks' ),
			'group'	=> 'data_labels_group',
			'type' => 'number',
			'small' => true,
			'inline' => true,
			'placeholder' => esc_html__( 'Normal', 'bricks' ),
			'required' => ['dataLabelsDisplay', '=', 'true'],
		];

		$this->controls['dataLabelsFontFamily'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Font family', 'bricks' ),
			'group'	=> 'data_labels_group',
			'type' => 'typography',
			'required' => ['dataLabelsDisplay', '=', 'true'],
			'exclude' => [
                'color',
                'text-align',
                'text-decoration',
                'text-transform',
                'letter-spacing',
                'white-space',
                'text-shadow',
                'text-wrap',
                'font-variation-settings',
				'font-size',
				'font-weight',
            ],
		];


		  /*   spacing */

		  $this->controls['spacingSeperator'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Padding', 'bricks' ),
			'group'	=> 'spacing_group',
			'type' => 'separator',
		  ];

		  $this->controls['spacingTop'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Top', 'bricks' ),
			'group'	=> 'spacing_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
		  ];

		  $this->controls['spacingRight'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Right', 'bricks' ),
			'group'	=> 'spacing_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
		  ];

		  $this->controls['spacingBottom'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Bottom', 'bricks' ),
			'group'	=> 'spacing_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
		  ];

		  $this->controls['spacingLeft'] = [
			'tab' => 'content',
			'label' => esc_html__( 'Left', 'bricks' ),
			'group'	=> 'spacing_group',
			'type' => 'number',
			'units' => false,
			'inline' => true,
			'small' => true,
			'placeholder' => esc_html__( '0', 'bricks' ),
		  ];

  }

  // Methods: Frontend-specific
  public function enqueue_scripts() {

	if ( bricks_is_builder_main() ) {
		return;
	  }

	wp_enqueue_script( 'x-chart-lib', BRICKSEXTRAS_URL . 'components/assets/js/chart.js', '', \BricksExtras\Plugin::VERSION, true );
	wp_enqueue_script( 'x-chart', BRICKSEXTRAS_URL . 'components/assets/js/' . \BricksExtras\Helpers::maybeMinifyScripts('dynamicchart') . '.js', '', \BricksExtras\Plugin::VERSION, true );

	if (!self::$script_localized) {

		wp_localize_script(
			'x-chart',
			'xChart',
			[
				'Instances' => [],
				'Config'	=> [],
				'RTL' => is_rtl() ? 'true' : 'false'
			]
		);

		self::$script_localized = true;

	}

	if (! \BricksExtras\Helpers::elementCSSAdded($this->name) ) {
		wp_enqueue_style( 'x-chart', BRICKSEXTRAS_URL . 'components/assets/css/dynamicchart.css', [], '' );
	}


  }


  public function render() {

		$settings = $this->settings;

		$content_items = ! empty( $settings['content_items'] ) ? $settings['content_items'] : false;
		$datasetItems = ! empty( $settings['datasetItems'] ) ? $settings['datasetItems'] : false;
		$chartType = isset( $settings['chart_type'] ) ? $settings['chart_type'] : 'bar';
		$loopDataPoints = isset( $settings['queryLoopDatapoints'] ) ? 'dataPoints' === $settings['queryLoopDatapoints'] : false;
		$loopNestedArrays = isset( $settings['queryLoopDatapoints'] ) ? 'array' === $settings['queryLoopDatapoints'] : false;

		$config = [
			'legendDisplay' => isset( $settings['legendDisplay'] ) ? $settings['legendDisplay'] : 'false',
			'chartType' => $chartType,
			'suggestedMin' => isset( $settings['suggestedMin'] ) ? $settings['suggestedMin'] : null,
			'suggestedMax' => isset( $settings['suggestedMax'] ) ? $settings['suggestedMax'] : null,
			'xGridColor' => isset( $settings['xGridColor'] ) ? $settings['xGridColor'] : '#ccc',
			'yGridColor' => isset( $settings['yGridColor'] ) ? $settings['yGridColor'] : '#ccc',
			'xBorderColor' => isset( $settings['xBorderColor'] ) ? $settings['xBorderColor'] : '#222',
			'yBorderColor' => isset( $settings['yBorderColor'] ) ? $settings['yBorderColor'] : '#222',
			'yAxisType' => isset( $settings['yAxisType'] ) ? $settings['yAxisType'] : 'linear',
			'xAxisTitle' => ! empty( $settings['xAxisTitle'] ) ? esc_attr( $settings['xAxisTitle'] ) : '',
			'yAxisTitle' => ! empty( $settings['yAxisTitle'] ) ? esc_attr( $settings['yAxisTitle'] ) : '',
			'beginAtZero' => isset( $settings['beginAtZero'] ) ? $settings['beginAtZero'] : 'true',
			'tension' => isset( $settings['tension'] ) ? floatval($settings['tension']) : 0.1,
			'barBorderRadius' => isset( $settings['barBorderRadius'] ) ? floatval($settings['barBorderRadius']) : 5,
			'barBorderWidth' => isset( $settings['barBorderWidth'] ) ? floatval($settings['barBorderWidth']) : 0,
			'barBorderColor' => isset( $settings['barBorderColor'] ) ? $settings['barBorderColor'] : '',
			'lineColor' => isset( $settings['lineColor'] ) ? $settings['lineColor'] :'#222',
			'lineWidth' => isset( $settings['lineWidth'] ) ? floatval($settings['lineWidth']) : 2,
			'linePointRadius' => isset( $settings['linePointRadius'] ) ? floatval($settings['linePointRadius']) : 3,
			'linePointBorderWidth' => isset( $settings['linePointBorderWidth'] ) ? floatval($settings['linePointBorderWidth']) : 1,
			'xAxisTitleColor' => isset( $settings['xAxisTitleColor'] ) ? $settings['xAxisTitleColor'] : '',
			'yAxisTitleColor' => isset( $settings['yAxisTitleColor'] ) ? $settings['yAxisTitleColor'] : '',
			'xAxisTicksColor' => isset( $settings['xAxisTicksColor'] ) ? $settings['xAxisTicksColor'] : '',
			'yAxisTicksColor' => isset( $settings['yAxisTicksColor'] ) ? $settings['yAxisTicksColor'] : '',
			'xAxisUnits' => isset( $settings['xAxisUnits'] ) ? esc_attr( $settings['xAxisUnits'] ) : '',
			'xAxisUnitPosition' => isset( $settings['xAxisUnitPosition'] ) ? $settings['xAxisUnitPosition'] : 'after',
			'ariaLabel' => isset( $settings['ariaLabel'] ) ? esc_attr( $settings['ariaLabel'] ) : esc_attr__( 'Chart', 'bricksextras' ),
			'rotation' => isset( $settings['rotation'] ) ? floatval( $settings['rotation'] ) : 0,
			'circumference' => isset( $settings['circumference'] ) ? floatval( $settings['circumference'] ) : 360, 
			'pieBorderColor' => isset( $settings['pieBorderColor'] ) ? $settings['pieBorderColor'] :'#222',
			'pieBorderWidth' => isset( $settings['pieBorderWidth'] ) ? floatval($settings['pieBorderWidth']) : 0,
			'pieCutOut' => isset( $settings['pieCutOut'] ) ? floatval( $settings['pieCutOut'] ) : 0,
			'legendPadding' => isset( $settings['legendPadding'] ) ? floatval( $settings['legendPadding'] ) : 0,
			'chartDirection' => isset( $settings['chartDirection'] ) ? $settings['chartDirection'] : 'vertical',
			'stacked' => isset( $settings['stacked'] ) ? $settings['stacked'] : 'disable',
			'reverseDatasets' => isset( $settings['reverseDatasets'] ),
			'tooltipDisplay' => isset( $settings['tooltipDisplay'] ) ? $settings['tooltipDisplay'] : 'false',
			'events' => isset( $settings['events'] ) ? $settings['events'] : 'click',
			'tooltipInteractionMode' => isset( $settings['tooltipInteractionMode'] ) ? $settings['tooltipInteractionMode'] : 'default',
			'tooltipIntersect' => isset( $settings['tooltipIntersect'] ) ? $settings['tooltipIntersect'] : 'default',
			'tooltipColor' => isset( $settings['tooltipColor'] ) ? $settings['tooltipColor'] : '#222',
			'tooltipBackground' => isset( $settings['tooltipBackground'] ) ? $settings['tooltipBackground'] : '#fff',
			'tooltipBorderColor' => isset( $settings['tooltipBorderColor'] ) ? $settings['tooltipBorderColor'] : '#222',
			'tooltipBorderWidth' => isset( $settings['tooltipBorderWidth'] ) ? floatval($settings['tooltipBorderWidth'] ) : 1,
			'tooltipCaretSize' => isset( $settings['tooltipCaretSize'] ) ? floatval($settings['tooltipCaretSize'] ) : 5,
			'loopDataPoints' => $loopDataPoints,
			'spacingTop' => isset( $settings['spacingTop'] ) ? floatval( $settings['spacingTop'] ) : 0,
			'spacingRight' => isset( $settings['spacingRight'] ) ? floatval( $settings['spacingRight'] ) : 0,
			'spacingBottom' => isset( $settings['spacingBottom'] ) ? floatval( $settings['spacingBottom'] ) : 0,
			'spacingLeft' => isset( $settings['spacingLeft'] ) ? floatval( $settings['spacingLeft'] ) : 0,

			'chartTypography' => isset( $settings['chartTypography'] ) ? $settings['chartTypography'] : array(),
			'xAxisFont'        => ! empty( $settings['xAxisFont'] ) ? $settings['xAxisFont'] : array(),
			'xAxisTicksFont'   => ! empty( $settings['xAxisTicksFont'] ) ? $settings['xAxisTicksFont'] : array(),
			'yAxisFont'        => ! empty( $settings['yAxisFont'] ) ? $settings['yAxisFont'] : array(),
			'yAxisTicksFont'   => ! empty( $settings['yAxisTicksFont'] ) ? $settings['yAxisTicksFont'] : array(),
			'legendFont'       => ! empty( $settings['legendFont'] ) ? $settings['legendFont'] : array(),
			'legendColor'	  => ! empty( $settings['legendColor'] ) ? $settings['legendColor'] : '#222',
			'tooltipTitleFont' => ! empty( $settings['tooltipTitleFont'] ) ? $settings['tooltipTitleFont'] : array(),
			'tooltipBodyFont'  => ! empty( $settings['tooltipBodyFont'] ) ? $settings['tooltipBodyFont'] : array()
		];

		if ( isset( $settings['chartAspectRatio'] ) ) {
			$config += [
				'aspectRatio' => $settings['chartAspectRatio']
			];
		}

		if ( isset( $settings['legendPosition'] ) ) {
			$config += [
				'legendPosition' => $settings['legendPosition']
			];
		}

		if ( isset( $settings['legendAlign'] ) ) {
			$config += [
				'legendAlign' => $settings['legendAlign']
			];
		}

		if ( isset( $settings['legendBoxWidth'] ) ) {
			$config += [
				'legendBoxWidth' => floatval( $settings['legendBoxWidth'] )
			];
		}

		if ( isset( $settings['legendBoxHeight'] ) ) {
			$config += [
				'legendBoxHeight' => floatval( $settings['legendBoxHeight'] )
			];
		}

		if ( isset( $settings['dataLabelsDisplay'] ) && 'true' === $settings['dataLabelsDisplay'] ) {
			$config += [
				'dataLabelsDisplay' => true,
				'dataLabelsAlign' => isset( $settings['dataLabelsAlign'] ) ? $settings['dataLabelsAlign'] : 'center',
				'dataLabelsAnchor' => isset( $settings['dataLabelsAnchor'] ) ? $settings['dataLabelsAnchor'] : 'center',
				'dataLabelsOffset' => isset( $settings['dataLabelsOffset'] ) ? floatval( $settings['dataLabelsOffset'] ) : 4,
				'dataLabelsPadding' => isset( $settings['dataLabelsPadding'] ) ? floatval( $settings['dataLabelsPadding'] ) : 6,
				'dataLabelsColor' => isset( $settings['dataLabelsColor'] ) ? $settings['dataLabelsColor'] : '#ffffff',
				'dataLabelsBackgroundColor' => isset( $settings['dataLabelsBackgroundColor'] ) ? $settings['dataLabelsBackgroundColor'] : false,
				'dataLabelsBorderColor' => isset( $settings['dataLabelsBorderColor'] ) ? $settings['dataLabelsBorderColor'] : false,
				'dataLabelsBorderWidth' => isset( $settings['dataLabelsBorderWidth'] ) ? floatval( $settings['dataLabelsBorderWidth'] ) : 0,
				'dataLabelsBorderRadius' => isset( $settings['dataLabelsBorderRadius'] ) ? floatval( $settings['dataLabelsBorderRadius'] ) : 0,
				'dataLabelsFontSize' => isset( $settings['dataLabelsFontSize'] ) ? floatval( $settings['dataLabelsFontSize'] ) : false,
				'dataLabelsFontWeight' => isset( $settings['dataLabelsFontWeight'] ) ? floatval( $settings['dataLabelsFontWeight'] ) : false,
				'dataLabelsFontFamily'=> isset( $settings['dataLabelsFontFamily'] ) ? $settings['dataLabelsFontFamily'] : false,
				'dataLabelsRotation' => isset( $settings['dataLabelsRotation'] ) ? floatval( $settings['dataLabelsRotation'] ) : 0,
			];
		}

		if ( 'doughnut' == $chartType ) {
			$xAxisDisplay = 'false';
			$yAxisDisplay = 'false';
		} else {
			$xAxisDisplay = !isset( $settings['xAxisDisplay'] ) ? 'true' : $settings['xAxisDisplay'];
			$yAxisDisplay = !isset( $settings['yAxisDisplay'] ) ? 'true' : $settings['yAxisDisplay'];
		}

		$config += [ 
			'xAxisDisplay' => $xAxisDisplay,
			'yAxisDisplay' => $yAxisDisplay,
		];
		

		$this->set_attribute( 'x-dynamic-chart_table', 'class', 'x-dynamic-chart_table' );
		$this->set_attribute( 'x-dynamic-chart_table', 'style', 
			'display: none;'
		);
		$this->set_attribute( 'x-dynamic-chart_table', 'data-x-chart', wp_json_encode( $config ) );

		// Generate and set a unique identifier for this instance
		$indentifier = \BricksExtras\Helpers::set_identifier_attribute( $this );
		
		$this->set_attribute( '_root', 'data-x-dynamic-chart', $chartType );
 
		echo "<div {$this->render_attributes( '_root' )}>";

		echo "<table {$this->render_attributes( 'x-dynamic-chart_table' )}>";

		if ( $loopNestedArrays ) {
	
		// Query Loop for nested arrays - collect all data first
		if ( isset( $settings['hasLoop'] ) ) {
			$query = new \Bricks\Query( [
				'id'       => $this->id,
				'settings' => $settings,
			] );

			if ( $query->count === 1 ) {
				// No results
				$no_results_content = $query->get_no_results_content();
				if ( empty( $no_results_content ) ) {
					echo $this->render_element_placeholder( ['title' => esc_html__( 'No results', 'bricks' )] );
				}
			} else {
				
				// Initialize collection array
				$this->flat_datapoints = [];
				
				// Use query loop to collect data via callback
				$query->render( [ $this, 'collect_flat_array_data' ], [] );
				
				// Collect all unique X-axis labels from all datasets
				$all_labels = [];
				foreach ( $this->flat_datapoints as $dataset_data ) {
					foreach ( $dataset_data['labels'] as $label ) {
						if ( ! in_array( $label, $all_labels ) ) {
							$all_labels[] = $label;
						}
					}
				}
				
				// Build datasets array with proper alignment
				$datasets = [];
				foreach ( $this->flat_datapoints as $dataset_name => $dataset_data ) {
					// For single dataset charts (empty dataset name), use datasetLabel if available
					if ( empty( $dataset_name ) && ! empty( $settings['datasetLabel'] ) ) {
						$legend_text = bricks_render_dynamic_data( $settings['datasetLabel'] );
					} else {
						$legend_text = $dataset_name;
					}
					
					// Remap data to align with all labels
					$remapped_data = [];
					foreach ( $all_labels as $label ) {
						$index = array_search( $label, $dataset_data['labels'] );
						if ( $index !== false && isset( $dataset_data['values'][$index] ) ) {
							$remapped_data[] = $dataset_data['values'][$index];
						} else {
							$remapped_data[] = null;
						}
					}
					
					$datasets[] = [
						'label' => $dataset_name,
						'legend' => $legend_text,
						'data' => $remapped_data, 
						'color' => ! empty( $dataset_data['color'] ) ? $dataset_data['color'] : ''
					];
				}
				
				// Render table with collected data
				echo $this->render_flat_array_table( $all_labels, $datasets, $settings );
			}
			
			$query->destroy();
			unset( $query );
		}
	
	} elseif ( !$loopDataPoints ) {

		/* table header */
		if ( $content_items ) {			

			echo "<thead><tr>";

			foreach ( $content_items as $index => $content_item ) { 

				$header_title = ! empty( $content_item['title'] ) ? esc_html( bricks_render_dynamic_data($content_item['title']) ) : '';

				echo '<th class="label">' . $header_title . '</th>';

			}

			echo "</tr></thead>";

		}

		/* table content */

		echo "<tbody>";

		// Query Loop
		if ( isset( $settings['hasLoop'] ) ) {
			$query = new \Bricks\Query( [
				'id'       => $this->id,
				'settings' => $settings,
			] );

			if ( $query->count === 1 ) {
				// No results: Empty by default (@since 1.4)
				$no_results_content = $query->get_no_results_content();

				if ( empty( $no_results_content ) ) {
					echo $this->render_element_placeholder( ['title' => esc_html__( 'No results', 'bricks' )] );
				}
			}
		}
		

		// Query Loop
		if ( isset( $settings['hasLoop'] ) ) {
			if (!!$content_items) {
				$content_item = $content_items[0];
				echo wp_kses_post( $query->render( [ $this, 'render_repeater_item' ], compact( 'content_items' ) ) );
			}
			$query->destroy();
			unset( $query );
		} else {

			// just output one row if not using query loop, so user can build single static chart
			?> <tr> <?php	

				$datasetLabels = ! empty( $settings['datasetLabel'] ) ? esc_attr( $settings['datasetLabel'] ) : '';

				if ( ! empty( $datasetLabels) ) {		
						echo '<td class="legend">';
						echo $datasetLabels;
						echo '</td>';	
				}
			
				if ( $content_items ) {			

					foreach ( $content_items as $index => $content_item ) {

						$style_config = [];

						if ( isset( $content_item['color'] ) ) {

							$style_config += [
								'color' => isset( $content_item['color'] ) ? $content_item['color'] : '',
							];

						}

						$this->set_attribute( "td-$index", 'data-x-chart-style', wp_json_encode( $style_config ) );
				
						echo "<td {$this->render_attributes( "td-$index" )}>";

						$content_datas = ! empty( $content_item['data'] ) ? esc_attr( bricks_render_dynamic_data( $content_item['data'] ) ) : '';

						if ( is_numeric( $content_datas ) ) {
							if ( !is_float( $content_datas) ) {
								$content_datas = number_format( $content_datas, 2, '.', '' );
							}
						}

						echo $content_datas;

						echo '</td>';

					}			

				}
			
			?> </tr> <?php
		}

		echo "</tbody>";  

		} else {

			echo "<thead><tr>";

			// Query Loop
			if ( isset( $settings['hasLoop'] ) ) {
				$query = new \Bricks\Query( [
					'id'       => $this->id,
					'settings' => $settings,
				] );

				if ( $query->count === 1 ) {
					// No results: Empty by default (@since 1.4)
					$no_results_content = $query->get_no_results_content();

					if ( empty( $no_results_content ) ) {
						echo $this->render_element_placeholder( ['title' => esc_html__( 'No results', 'bricks' )] );
					}
				} else {

					echo wp_kses_post( $query->render( [ $this, 'render_repeater_head' ], compact( 'content_items' ) ) );
				
					$query->destroy();
					unset( $query );

				}
			}

			echo "</tr></thead>";

			echo '<tbody><tr>'; 

				echo "<td class=legend ";
				
				echo ">";

				$datasetLabel = ! empty( $settings['datasetLabel'] ) ? wp_kses_post( $settings['datasetLabel'] ) : '';

				echo $datasetLabel; 

				echo '</td>';

				// Query Loop
				if ( isset( $settings['hasLoop'] ) ) {
					$query = new \Bricks\Query( [
						'id'       => $this->id,
						'settings' => $settings,
					] );

					if ( $query->count === 1 ) {
						// No results: Empty by default (@since 1.4)
						$no_results_content = $query->get_no_results_content();

						if ( empty( $no_results_content ) ) {
							echo $this->render_element_placeholder( ['title' => esc_html__( 'No results', 'bricks' )] );
						}
					} else {

						echo wp_kses_post( $query->render( [ $this, 'render_repeater_body' ], compact( 'content_items' ) ) );
					
						$query->destroy();
						unset( $query );

					}
				}
			
			echo '</tr></tbody>';

		}

		/* datasetSettings */
		if ( $datasetItems && isset( $settings['hasLoop'] ) ) {			

			echo "<ul class='x-dynamic-chart_list' style='display: none;'>";

			foreach ( $datasetItems as $index => $datasetItem ) {

				$color = ! empty( $datasetItem['color'] ) ? $datasetItem['color'] : '';

				$dataset_config = [];

				if ( isset( $datasetItem['color'] ) ) {

					$dataset_config += [
						'color' => isset( $datasetItem['color'] ) ? $datasetItem['color'] : '',
					];

				}

				$this->set_attribute( "li-$index", 'data-x-chart-style', wp_json_encode( $dataset_config ) );

				echo "<li {$this->render_attributes( "li-$index" )} ></li>";

			}

			echo "</ul>";

		}

		echo "</table>";
		echo "</div>";

		
    
  }

  public function render_repeater_item( $content_items ) {

	$settings = $this->settings;
	$index    = $this->loop_index;

	// Render
	ob_start();
	?>
	<tr>
	<?php	

		$datasetLabels = ! empty( $settings['datasetLabel'] ) ? $settings['datasetLabel'] : '';
		$datasetColor = ! empty( $settings['datasetColor'] ) ? $settings['datasetColor'] : '';

		echo "<td class=legend ";
		
		if ( isset( $settings['maybeDynamicColors'] ) ) {
			echo 'dynamic' === $settings['maybeDynamicColors'] ? "data-set-color=" . esc_attr( $datasetColor ) . " " : "";
		}
		
		echo ">";
		echo esc_attr( bricks_render_dynamic_data($datasetLabels) );
		echo '</td>';	



		if ( $content_items ) {			

			foreach ( $content_items as $index => $content_item ) {

				echo '<td>';

				$content_datas = ! empty( $content_item['data'] ) ? bricks_render_dynamic_data( $content_item['data'] ) : '';

				echo esc_attr( $content_datas );

				echo '</td>';

			}			

		}
	
	?>
  	</tr>
	<?php
	$html = ob_get_clean();

	$this->loop_index++;

	return $html;

  }

  public function render_repeater_head( $content_items ) {

	$settings = $this->settings;
	$index    = $this->loop_index;

	// Render
	ob_start();

	$queryLoopDatapoints_Label = ! empty( $settings['queryLoopDatapoints_Label'] ) ? esc_html( bricks_render_dynamic_data( $settings['queryLoopDatapoints_Label'] ) ) : '';

	echo '<th class="label" ';

	$datasetColor = ! empty( $settings['datasetColor'] ) ? $settings['datasetColor'] : '';

	if ( isset( $settings['maybeDynamicColors'] ) ) {
		echo 'dynamic' === $settings['maybeDynamicColors'] ? "data-set-color=" . esc_attr( $datasetColor ) . " " : "";
	}
	
	echo '>' . $queryLoopDatapoints_Label . '</th>';

	$html = ob_get_clean();

	$this->loop_index++;

	return $html;

  }

  public function render_repeater_body( $content_items ) {

	$settings = $this->settings;
	$index    = $this->loop_index;

	// Render
	ob_start();

	$queryLoopDatapoints_Value = ! empty( $settings['queryLoopDatapoints_Value'] ) ? bricks_render_dynamic_data( $settings['queryLoopDatapoints_Value'] ) : '';

	echo "<td ";

	$dataPointColor = isset( $settings['datasetColor'] ) ? $settings['datasetColor'] : '';
	
	if ( isset( $settings['maybeDynamicColors'] ) ) {
		if ( 'dynamic' === $settings['maybeDynamicColors'] ) {
			echo "data-point-color='" . esc_attr( $dataPointColor ) . "' ";
		}
	}
	
	echo ">" . esc_html( $queryLoopDatapoints_Value ) . "</td>";

	$html = ob_get_clean();

	$this->loop_index++;

	return $html;

  }

  public function collect_flat_array_data( $args = [] ) {
	
	$settings = $this->settings;
	
	// Render dynamic tags for this datapoint with defaults
	$dataset_identifier = ! empty( $settings['flatArray_DatasetIdentifier'] ) ? bricks_render_dynamic_data( $settings['flatArray_DatasetIdentifier'] ) : '';
	$xaxis_label = ! empty( $settings['flatArray_XAxisLabel'] ) ? bricks_render_dynamic_data( $settings['flatArray_XAxisLabel'] ) : bricks_render_dynamic_data( '{query_array @key:\'datapoint\'}' );
	$value = ! empty( $settings['flatArray_Value'] ) ? bricks_render_dynamic_data( $settings['flatArray_Value'] ) : bricks_render_dynamic_data( '{query_array @key:\'value\'}' );

	if ( empty( $xaxis_label ) || $value === '' ) {
		return '';
	}
	
	// If dataset identifier is empty, use empty string for single dataset charts
	if ( empty( $dataset_identifier ) ) {
		$dataset_identifier = '';
	}  
	
	// Store this datapoint grouped by dataset identifier
	if ( ! isset( $this->flat_datapoints[ $dataset_identifier ] ) ) {
		// Get color using existing datasetColor control (if dynamic colors enabled)
		$color = '';
		if ( isset( $settings['maybeDynamicColors'] ) && 'dynamic' === $settings['maybeDynamicColors'] && ! empty( $settings['datasetColor'] ) ) {
			$color = bricks_render_dynamic_data( $settings['datasetColor'] );
		}
		
		$this->flat_datapoints[ $dataset_identifier ] = [
			'labels' => [],
			'values' => [],
			'color' => $color
		];
	}
	
	$this->flat_datapoints[ $dataset_identifier ]['labels'][] = esc_html( $xaxis_label );
	$this->flat_datapoints[ $dataset_identifier ]['values'][] = floatval( $value );
	
	return '';
	
  }

  public function render_flat_array_table( $labels, $datasets, $settings ) {
	
	ob_start();
	 
	// Build thead with X-axis labels as columns
	echo "<thead><tr>";
	
	foreach ( $labels as $label ) {
		echo '<th class="label">' . esc_html( $label ) . '</th>';
	}
	
	echo "</tr></thead>";
	
	// Build tbody with datasets as rows
	echo "<tbody>";
	
	foreach ( $datasets as $dataset ) {
		echo '<tr>';
		
		// First cell is the legend (use legend setting or dataset label as fallback)
		echo '<td class="legend"';
		
		// Add color data attribute if dynamic colors enabled
		if ( isset( $settings['maybeDynamicColors'] ) && 'dynamic' === $settings['maybeDynamicColors'] && ! empty( $dataset['color'] ) ) {
			echo ' data-set-color="' . esc_attr( $dataset['color'] ) . '"';
		}
		
		echo '>';
		echo esc_html( isset( $dataset['legend'] ) ? $dataset['legend'] : $dataset['label'] );
		echo '</td>';
		
		// Output data values for each X-axis point
		foreach ( $dataset['data'] as $value ) {
			echo '<td>';
			echo esc_html( $value );
			echo '</td>';
		}
		
		echo '</tr>';
	}
	
	echo "</tbody>";
	
	return ob_get_clean();

  }

}