<?php

/**
 * B2ichart Shortcode
 * @since 0.1.0
 * @package b2ichart
 */

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

/**
 * B2ichart Shortcode.
 * @since 0.1.0
 */
class B2ichart_Shortcode {
	
	protected $version = '0.1';
	
	/**
	 * Parent plugin class
	 * @var   class
	 * @since 0.1.0
	 */
	protected $plugin = null;

	/**
	 * Business ID option
	 * @var   string
	 * @since 0.1.0
	 */
	protected $business_id = '';

	/**
	 * Key option
	 * @var   string
	 * @since 0.1.0
	 */
	protected $key = '';
	protected $ticker= '';
	/**
	***************************************************************************************************************
	 * Base URL
	 * @var   string
	 * @since 0.1.0
	***************************************************************************************************************
	*/
	 
	protected $base_http = 'https';
	protected $base_url = 'https://www.b2i.us/b2i/';
	protected $base_url2 = '://www.b2i.us/b2i/';
	protected $base_url3 = '://www.b2i.us/';
	protected $base_url2_test = '://192.168.1.55/b2i/';
	protected $base_url3_test = '://192.168.1.55/';
	
	/**
	*********************************************************************************
	 * Constructor
	 *
	 * @since  0.1.0
	 * @param  object $plugin Main plugin object.
	 * @return void
	*********************************************************************************
	 */
	public function __construct( $plugin ) {
		$this->plugin = $plugin;

		add_action( 'cmb2_init', array( $this, 'setup_vars' ) );
		//add_action('cmb2_init', array( $this, 'b2i_shortcode_wp_register_scripts' ) );
		add_action( 'cmb2_init', array( $this, 'shortcodes' ) );
		
	}

	
	
	/**
	*********************************************************************************
	 * Setup business id and api key variables
	 * @since  0.1.0
	 * @return void
	*********************************************************************************
	*/
	public function setup_vars() {
		$this->business_id = cmb2_get_option( 'b2ichart_options', 'business_id' );
		$this->key = cmb2_get_option( 'b2ichart_options', 'key' );
		$this->ticker = cmb2_get_option( 'b2ichart_options', 'ticker' );
	}
	
	
	
	
	/**
	 *******************************************************************************
	 * Initiate shortcodes
	 * @since  0.1.0
	 * @return void
	 ********************************************************************************
	 */
	public function shortcodes() {
		add_shortcode( 'b2i_chart', array( $this, 'chart_shortcode' ) );
		add_shortcode( 'b2i_intrachart', array( $this, 'intradaychart_shortcode' ) );
		add_shortcode( 'b2i_chart2', array( $this, 'chart_highchart_shortcode' ) );
		add_shortcode( 'b2i_chart5', array( $this, 'chart_amchart5_shortcode' ) );
	}




	/**
	*********************************************************************************
	 * Define the High Chart shortcode 
	 * @since  1.7
	 * @return string
	*********************************************************************************
	 */
	public function chart_highchart_shortcode( $atts ) {
		$html = '';
		$dollarsign='';
		$atts = shortcode_atts(
			array(
				'sdiv' => 'highchartdiv',
				'h' => 'https',
				'bizid' => '',
				'api' => '',
				's' => '',
				'sd' => '',
				'height' => '500px',
				'width' => '96%',
				'lang' => '1',
				'ds' => '1',
				'c' => '003399',
				'c2' => '00ff00',
				'c3' => '7CB5EB',
				'cf1' => '7CB5EB',
				'cf2' => 'FFFFFF',
				'bgc' => 'FFFFFF',
				'ct' => 'area',
				'vct' => 'column',
				'vc' => '7cb5ec',
				
				'p' => '3m',
				'e' => '',
				'vls' => 'left',
				'bga' => '1.0',
				'cbc' => '999999',
				'cba' => '0.5',
				'bs' => '13',
				'bc' => '003399',
				'lc' => '666666',
				'ls' => '15',
				'lc2' => '666666',
				'ls2' => '11',
				'lc3' => '333333',
				'ls3' => '20',
				'la3' => '0.2',
				'cc' => '003399',
				'sp' => '1',
				'sc' => '1',
				'ssb' => '1',
				'sv' => '1',
				'lm' => '13',
				'rm' => '5',
				'tm' => '5',
				'bm' => '5',
				'vic' => '1',
				'vfa' => '.9',
				'fa' => '0.2',
				'ca' => '0.1',
				'stl' => '1',
				'sil' => '1',
				'lt' => '2',
				'ps' => 'bottom',
				'centsep' => '.',
				'thoussep' => ',',
				'volumetext' => 'Volume',
				'pricetext' => 'Price',
				'ga' => '.2',
				'gc' => 'bbbbbb',
				'udg' => '0',
				'ulc' => '4CAF50',
				'ufc' => '4CAF50',
				'dlc' => 'ff0000',
				'dfc' => 'ff0000'
			),
			$atts
		);


$bizid = $this->business_id;
$api = $this->key;
$ticker = $this->ticker;

if(esc_attr( $atts['bizid'])!=''){
	$bizid = esc_attr( $atts['bizid']);
}

if(esc_attr( $atts['api'])!=''){
	$api = esc_attr( $atts['api']);
}

if(esc_attr( $atts['s'])!=''){
	$ticker = esc_attr( $atts['s']);
}


if($atts['sd']=='')$atts['sd'] = $ticker ;
switch ($atts['ds']) {
  case '0':
	$dollarsign='';
	break;
  case '1':
	$dollarsign='$';
	break;
  case '2':
	$dollarsign='\u20AC';
	break;
  case '3':
	$dollarsign='\u00A3';
	break;
  case '4':
	$dollarsign='\u20AA';
	break;
  case '5':
    $dollarsign='৳';
    break;
}
// Base URL to your plugin folder
$jsbase_url = plugin_dir_url( __FILE__ );
$jsurl = esc_url( $jsbase_url . 'js/high/');

$html .= '<div id="' . esc_attr( $atts['sdiv'] ) . '" class="B2iChartContainer" style="height: ' . esc_attr( $atts['height'] ) . '; width:' . esc_attr( $atts['width'] ) . ';"></div>' . "\n";
//$html .= '<script src="https://stockcharting.s3.amazonaws.com/highstock.js" type="text/javascript"></script>' . "\n";
//$html .= '<script src="https://stockcharting.s3.amazonaws.com/data.js" type="text/javascript"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'highcharts.js"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'stock.js"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'annotations-advanced.js"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'full-screen.js"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'price-indicator.js"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'stock-tools.js"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'exporting.js"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'export-data.js"></script>' . "\n";    
$html .= '<script src="' . $jsurl . 'hollowcandlestick.js"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'accessibility.js"></script>' . "\n";
$html .= '<script type="text/javascript">' . "\n";
$html .= 'Highcharts.getJSON("https://www.b2i.us/profiles/inc/jsonstockdata.asp?api=' . $api . '&b=' . $bizid . '&s=' . $ticker . '", function (data) {' . "\n";
$html .= '    // split the data set into ohlc and volume
    var ohlc = [],
        volume = [],
        dataLength = data.length,
        i = 0;

    for (i; i < dataLength; i += 1) {
        ohlc.push([
            data[i][0], // the date
            data[i][1], // open
            data[i][2], // high
            data[i][3], // low
            data[i][4] // close
        ]);

        volume.push([
            data[i][0], // the date
            data[i][5] // the volume
        ]); } ' . "\n";

$html .= 'Highcharts.stockChart("' . esc_attr( $atts['sdiv'] ) . '", { ' . "\n";
$html .= "yAxis: [{
            height: '80%',
            resize: {
                enabled: true
            }
        }, {
            top: '80%',
            height: '20%',
            offset: 0
        }],
        tooltip: {
            shape: 'square',
            headerShape: 'callout',
            borderWidth: 0,
            shadow: false,
            positioner: function (width, height, point) {
                var chart = this.chart,
                    position;

                if (point.isHeader) {
                    position = {
                        x: Math.max(
                            // Left side limit
                            chart.plotLeft,
                            Math.min(
                                point.plotX + chart.plotLeft - width / 2,
                                // Right side limit
                                chart.chartWidth - width - chart.marginRight
                            )
                        ),
                        y: point.plotY
                    };
                } else {
                    position = {
                        x: point.series.chart.plotLeft,
                        y: point.series.yAxis.top - chart.plotTop
                    };
                }

                return position;
            }
        },
		plotOptions: {
			area: {
				color: '#" . esc_attr( $atts['bgc'] ) . "',
				fillColor: {
					linearGradient: {
						x1: 0,
						y1: 0,
						x2: 0,
						y2: 1
					},
					stops: [
						[0, '#" . $atts['cf1'] . "'],
						[1, '#" . $atts['cf2'] . "']
					]
				},
				marker: {
					enabled: false
				},
				lineWidth: 1,
				states: {
					hover: {
						lineWidth: 2
					}
				},
				threshold: null
			}
		},
        stockTools: {
            gui: {
                enabled: true
            }
        },
        series: [{
            type: '" . $atts['ct']  . "',
            id: 'Price',
            name: '" . $atts['sd']  . " " . $dollarsign  . "',
            data: ohlc,
			color: '#" . $atts['c'] . "', // Color for the falling candles
			upColor: '#" . $atts['c2'] . "', // Color for the rising candles
			lineColor: '#" . $atts['c3'] . "' // Color for the lines of the candles
        }, {
            type: '" . $atts['vct'] . "',
            id: 'Volume',
            name: '" . $atts['sd']  . " Volume',
            data: volume,
            yAxis: 1
			,color: '#" . $atts['vc'] . "', // Color for Volume
			lineColor: '#" . $atts['vc'] . "' // Color Volume line
        }],
		exporting: {
			enabled: true, // Enable exporting
			buttons: {
				contextButton: {
					menuItems: [
						'fullscreen', // Add print option
						'printChart', // Add print option
						'exportData', // Separator line
					]
				}
			}
		},
        rangeSelector: {
            selected: 1, // controls which period is selected - 0 based
        },
		navigation: {
			select: true, // This sets the default state as selected
		},
		navigator: {
			series: {
				color: '#999999', // Custom color for the navigator line
				lineWidth: 1 // Width of the navigator line
			}
		},
        responsive: {
            rules: [{
                condition: {
                    maxWidth: 800
                },
                chartOptions: {
                    rangeSelector: {
                        buttons: [{
                            type: 'day',
                            count: 10,
                            text: '10d',
							title: '10 days'
                        }, {
                            type: 'month',
                            count: 1,
                            text: '1m',
							title: '1 month'
                        }, {
                            type: 'month',
                            count: 3,
                            text: '3m',
							title: '3 months'
                        }, {
                            type: 'month',
                            count: 6,
                            text: '6m',
							title: '6 months'
                        }, {
                            type: 'year',
                            count: 1,
                            text: '1y',
							title: '1 year'
                        }, {
                            type: 'year',
                            count: 5,
                            text: '5y',
							title: '5 years'
                        }, {
                            type: 'all',
                            text: 'All',
							title: 'View all'
                        }],
                        selected: 1,
                        inputEnabled: true
                    }
                }
            }]
        }
    });" . "\n";
$html .= '}); ' . "\n";


$html .= '</script>' . "\n";

return $html;
}




	

	/**
	*********************************************************************************
	 * Define the Chart  plugin shortcode (Chart Version: 3)
	 * @since  1.7.
	 * @return string
	*********************************************************************************
	 */
	public function chart_amchart5_shortcode( $atts ) {
		$html = '';
		$dollarsign = '';
		$atts = shortcode_atts(
			array(
				'sdiv' => 'chartdiv',
				'h' => 'https',
				's' => '',
				'sd' => '',
				'e' => '',
				'ds' => '1',
				'lang' => '1',
				'c' => 'D42323',
				'lc' => '666666',
				'stl' => '1',
				'la' => '1', // label alpha
				'ls' => '15',
				'lc2' => '666666',
				'ls2' => '11',
				'lc3' => '333333',
				'ls3' => '20',
				'la3' => '0.2',
				'bgc' => 'ffffff',  // added 1
				'bga' => '1.0',
				'cbc' => '999999',  // added 2
				'cba' => '0.5',
				'bs' => '13',
				'bc' => '003399',
				'p' => '3m',
				'height' => '500px',
				'width' => '96%',
				'sp' => '1',
				'sc' => '1',
				'cc' => '003399',
				'cw' => '10',
				'ca' => '0.1',
				'ssb' => '1',
				'sbh' => '50',
				'sv' => '1',
				'lm' => '13',
				'rm' => '5',
				'tm' => '5',
				'bm' => '5',
				'vic' => '1',
				'ct' => 'LineSeries',
				'vls' => 'left',
				'vct' => 'ColumnSeries',
				'vfa' => '.9',
				'fa' => '0.2',
				'sil' => '1',
				'lt' => '2',
				'ps' => 'bottom',
				'centsep' => '.',
				'thoussep' => ',',
				'volumetext' => 'Volume',
				'pricetext' => 'Price',
				'ga' => '.2',
				'gc' => 'D42323',
				'udg' => '0',
				'mode' => '0',
				'ulc' => '4CAF50',
				'ufc' => '4CAF50',
				'dlc' => 'ff0000',
				'dfc' => 'ff0000'
			),
			$atts
		);

		if ($atts['sd'] == '')
			$atts['sd'] = $ticker;

		switch ($atts['ds']) {
			case '0':
				$dollarsign = '';
				break;
			case '1':
				$dollarsign = '$';
				break;
			case '2':
				$dollarsign = '\u20AC';
				break;
			case '3':
				$dollarsign = '\u00A3';
				break;
			case '4':
				$dollarsign = '\u20AA';
				break;
			case '5':
				$dollarsign = '৳';
				break;
		}

		$html .= '<div id="' . esc_attr($atts['sdiv']) . '" class="B2iChartContainer" style="height: ' . esc_attr($atts['height']) . '; width:' . esc_attr($atts['width']) . ';"></div>' . "\n";
		$html .= '<div id="chartcontrols" class="B2iChartContainer" style="height: ' . esc_attr($atts['height']) . '; width:' . esc_attr($atts['width']) . ';"></div>' . "\n";
		//$html .= '<script src="' . esc_url(esc_attr( $atts['h'] ) . $this->base_url3 . 'profiles/inc/amstockgetJsData.asp?b=' ) . $this->business_id . '&s=' . esc_attr( $atts['s'] ) . '" type="text/javascript"></script>' . "\n";

		$html .= '<style>
				#' . esc_attr($atts['sdiv']) . '{
					background-color: #' . esc_attr($atts['bgc']) . ';
					opacity: ' . esc_attr($atts['bga']) . ';
				}
			</style>' . "\n";
		$html .= '<script src="https://stockcharting.s3.amazonaws.com/index.js" type="text/javascript"></script>' . "\n";
		$html .= '<script src="https://stockcharting.s3.amazonaws.com/xy.js" type="text/javascript"></script>' . "\n";
		$html .= '<script src="https://stockcharting.s3.amazonaws.com/stock.js" type="text/javascript"></script>' . "\n";
		$html .= '<script src="https://stockcharting.s3.amazonaws.com/exporting.js" type="text/javascript"></script>' . "\n";
		$html .= '<script src="https://stockcharting.s3.amazonaws.com/moment.min.js" type="text/javascript"></script>' . "\n";
		
		$html .= '<style src="https://www.b2i.us/profiles/inc/CSS.asp?m=5&b=' . $this->business_id . '&api=' . $this->key . '" type="text/javascript"></style>' . "\n";
		$html .= '<script src="https://www.b2i.us/profiles/inc/amstockgetJsData2.asp?b=' . $this->business_id . '&api=' . $this->key . '&s=' . esc_attr($atts['s']) . '&m=' . esc_attr($atts['mode']) . '" type="text/javascript"></script>' . "\n";
				
		$html .= '<script type="text/javascript">' . "\n";
		$html .= 'var chartData = [];' . "\n";
		$html .= 'loadData();' . "\n";
		$html .= 'chartData.map((e) => { e.date = moment(e.date).valueOf() } ); console.log(chartData)' . "\n";

		$html .= 'var root = am5.Root.new("' . esc_attr($atts['sdiv']) . '");

			root.interfaceColors.set("background", am5.color(0x5086BF));
		
			var stockChart = root.container.children.push(am5stock.StockChart.new(root, {
				stockPositiveColor: am5.color(0x00A2FF),
				stockNegativeColor: am5.color(0x00A2FF),
				volumePositiveColor: am5.color(0x00A2FF),
				volumeNegativeColor: am5.color(0x00A2FF)
			}));
			
			root.numberFormatter.set("numberFormat", "#,###.00"); ' . "\n";

		if ($atts['stl'] == 1) {
			$html .= 'stockChart.children.unshift(am5.Label.new(root, {
					text: "' . esc_attr($atts['e']) . '",
					fontSize: ' . esc_attr($atts['ls']) . ',
					fontWeight: "500",
					textAlign: "left",
					x: am5.percent(50),
					centerX: am5.percent(50),
					paddingTop: 0,
					paddingBottom: 0,
					fill: am5.color(0x' . esc_attr($atts['lc']) . '),
					opacity: ' . esc_attr($atts['la']) . ',
				}
			));' . "\n";
		}

		$html .=
			'var mainPanel = stockChart.panels.push(
				am5stock.StockPanel.new(root, {
					wheelY: "zoomX",
					panX: true,
					panY: false,
					height: am5.percent(70),
				})
			);' . "\n";

		// FOR DASHGRID
		if (esc_attr($atts['udg']) == 1) {
			$html .= 'var volumeAxisRendererY = am5xy.AxisRendererY.new(root, {});
			var valueAxis = mainPanel.yAxes.push(
				am5xy.ValueAxis.new(root, {
					renderer: volumeAxisRendererY,
					extraMin: 0.1, // adds some space for for main series
					tooltip: am5.Tooltip.new(root, {}),
					numberFormat: "#' . esc_attr($atts['thoussep']) . '###' . esc_attr($atts['centsep']) . '00",
					extraTooltipPrecision: 2,
				})
			);
			volumeAxisRendererY.grid.template.set("strokeDasharray", [10]);
			volumeAxisRendererY.grid.template.set("strokeOpacity", ' . esc_attr($atts['ga']) . ' );
			volumeAxisRendererY.grid.template.set("stroke", am5.color(0x' . esc_attr($atts['gc']) . '));
			volumeAxisRendererY.grid.template.set("pan", "zoom");

			var volumeAxisRendererX = am5xy.AxisRendererX.new(root, {});
			var dateAxis = mainPanel.xAxes.push(
				am5xy.GaplessDateAxis.new(root, {
					baseInterval: {
						timeUnit: "day",
						count: 1,
					},
					renderer: volumeAxisRendererX,
					tooltip: am5.Tooltip.new(root, {}),
				})
			);
			volumeAxisRendererX.grid.template.set("strokeDasharray", [10]);
			volumeAxisRendererX.grid.template.set("strokeOpacity", ' . esc_attr($atts['ga']) . ' );
			volumeAxisRendererX.grid.template.set("stroke", am5.color(0x' . esc_attr($atts['gc']) . '));
			' . "\n";
		} else {
			$html .= 'var valueAxis = mainPanel.yAxes.push(
				am5xy.ValueAxis.new(root, {
					renderer: am5xy.AxisRendererY.new(root, {
						pan: "zoom",
					}),
					extraMin: 0.1, // adds some space for for main series
					tooltip: am5.Tooltip.new(root, {}),
					numberFormat: "#' . esc_attr($atts['thoussep']) . '###' . esc_attr($atts['centsep']) . '00",
					extraTooltipPrecision: 2,
				})
			);

			var dateAxis = mainPanel.xAxes.push(
				am5xy.GaplessDateAxis.new(root, {
					baseInterval: {
						timeUnit: "day",
						count: 1,
					},
					renderer: am5xy.AxisRendererX.new(root, {
						fill: am5.color(0xFF0000),
						stroke: am5.color(0xFF0000),
						strokeDasharray: [10]
					}),
					tooltip: am5.Tooltip.new(root, {}),
				})
			);
			' . "\n";
		}

		// END FOR DASHGRID

		$html .= 'var valueSeries = mainPanel.series.push(am5xy.' . ucwords(esc_attr($atts['ct'])) . '.new(root, {
					name: "' . $atts['sd'] . '",
					clustered: true,
					valueXField: "date",
					valueYField: "value",
					highValueYField: "high",
					lowValueYField: "low",
					openValueYField: "open",
					calculateAggregates: true,
					xAxis: dateAxis,
					yAxis: valueAxis,
					legendValueText: "open: [bold]{openValueY}[/] high: [bold]{highValueY}[/] low: [bold]{lowValueY}[/] close: [bold]{valueY}[/]",
					fillColors: "#FF0000",
  					strokeWidth: 200,
					minDistance: 1,
					tooltip: am5.Tooltip.new(root, {
						pointerOrientation: "horizontal",
						labelText: "{valueY}"
					})
				})
			);' . "\n";

		$html .= 'valueSeries.set("fill", am5.color(0xff0000));' . "\n";

		if (esc_attr($atts['ct']) != "ColumnSeries" || esc_attr($atts['ct']) != "CandlestickSeries") {
			$html .= '
				valueSeries.strokes.template.setAll({
					strokeWidth: ' . esc_attr($atts['lt']) . ',
    				color: am5.color(0x' . esc_attr($atts['c']) . '),
					strokeDasharray: []
				});' . "\n";
			$html .= '
				valueSeries.fills.template.setAll({
    				color: am5.color(0x' . esc_attr($atts['c']) . '),
				});' . "\n";
		}

		$html .=
			'stockChart.set("stockSeries", valueSeries);
			
			var valueLegend = mainPanel.plotContainer.children.push(
				am5stock.StockLegend.new(root, {
					stockChart: stockChart,
				})
			);
			
			var volumePanel = stockChart.panels.push(
				am5stock.StockPanel.new(root, {
					panX: true,
					panY: true,
					height: am5.percent(30),
					paddingTop: 6,
				})
			);
			
			volumePanel.panelControls.closeButton.set("forceHidden", true);

			var volumeDateAxis = volumePanel.xAxes.push(
				am5xy.GaplessDateAxis.new(root, {
					baseInterval: {
						timeUnit: "day",
						count: 1,
					},
					renderer: am5xy.AxisRendererX.new(root, {}),
					tooltip: am5.Tooltip.new(root, {
						forceHidden: true,
					}),
					height: 0,
				})
			);
			
			volumeDateAxis.get("renderer").labels.template.set("forceHidden", true);
			
			var volumeAxisRenderer = am5xy.AxisRendererY.new(root, {});

			var volumeValueAxis = volumePanel.yAxes.push(
				am5xy.ValueAxis.new(root, {
					numberFormat: "#' . esc_attr($atts['centsep']) . '#a",
					renderer: volumeAxisRenderer,
				})
			);
			
			var volumeSeries = volumePanel.series.push(am5xy.' . ucwords(esc_attr($atts['vct'])) . '.new(root, {
					name: "Volume",
					clustered: false,
					valueXField: "date",
					valueYField: "volume",
					xAxis: volumeDateAxis,
					yAxis: volumeValueAxis,
					legendValueText: "[bold]{valueY.formatNumber(\'#,###.0a\')}[/]",
				})
			);

			// TODO:: পরে
			// volumeSeries.columns.template.setAll({
			// 	strokeOpacity: 0,
			// 	fillOpacity: 0.5,
			// });
			
			// volumeSeries.columns.template.adapters.add("fill", function (fill, target) {
			// 	var dataItem = target.dataItem;
			// 	if (dataItem) {
			// 		return stockChart.getVolumeColor(dataItem);
			// 	}
			// 	return fill;
			// });
			
			var volumeLegend = volumePanel.plotContainer.children.push(
				am5stock.StockLegend.new(root, {
					stockChart: stockChart,
				})
			);
			
			stockChart.set("volumeSeries", volumeSeries);
			valueLegend.data.setAll([valueSeries]);
			volumeLegend.data.setAll([volumeSeries]);' . "\n";

		// ADD CURSOR TO MAIN PANEL
		// START OF MAIN PANEL CURSOR SETTINGS
		if (esc_attr($atts['sc']) == 1) {
			$html .= 'var cursor = mainPanel.set("cursor", am5xy.XYCursor.new(root, {
							yAxis: valueAxis,
							xAxis: dateAxis,
							snapToSeries: [valueSeries],
							snapToSeriesBy: "y!",
						}
					));

					cursor.lineX.setAll({
						stroke: am5.color(0x' . esc_attr($atts['cc']) . '),
						strokeWidth: ' . esc_attr($atts['cw']) . ',
						strokeDasharray: [],
						strokeOpacity: ' . esc_attr($atts['ca']) . ',
					});

					cursor.lineY.setAll({
						stroke: am5.color(0x' . esc_attr($atts['cc']) . '),
						strokeWidth: ' . esc_attr($atts['cw']) . ',
						strokeDasharray: [],
						strokeOpacity: ' . esc_attr($atts['ca']) . ',
					});

					' . "\n";
		}
		// END OF MAIN PANEL CURSOR SETTINGS

		// ADD CURSOR TO VOLUME
		// START OF VOLUME PANEL CURSOR SETTINGS
		if (esc_attr($atts['sv']) == 1) {
			$html .= 'var volumeCursor = volumePanel.set("cursor", am5xy.XYCursor.new(root, {
							yAxis: volumeValueAxis,
							xAxis: volumeDateAxis,
							snapToSeries: [volumeSeries],
							snapToSeriesBy: "y!",
							opacity: 1,
						})
					);

					volumeCursor.lineY.set("forceHidden", true);

					volumeCursor.lineX.setAll({
						stroke: am5.color(0x' . esc_attr($atts['cc']) . '),
						strokeWidth: ' . esc_attr($atts['cw']) . ',
						strokeDasharray: [],
						strokeOpacity: ' . esc_attr($atts['ca']) . ',
					});

					volumeCursor.lineY.setAll({
						stroke: am5.color(0x' . esc_attr($atts['cc']) . '),
						strokeWidth: ' . esc_attr($atts['cw']) . ',
						strokeDasharray: [],
						strokeOpacity: ' . esc_attr($atts['ca']) . ',
					});

					' . "\n";
		}
		// END OF VOLUME PANEL CURSOR SETTINGS

		// START OF SCROLLBAR
		if (esc_attr($atts['ssb']) == 1) {
			$html .=
				'var scrollbar = mainPanel.set("scrollbarX", am5xy.XYChartScrollbar.new(root, {
					orientation: "horizontal",
					height: ' . esc_attr($atts['sbh']) . ',
				})
			);
			stockChart.toolsContainer.children.push(scrollbar);    

			var sbDateAxis = scrollbar.chart.xAxes.push(am5xy.GaplessDateAxis.new(root, {
					baseInterval: {
						timeUnit: "day",
						count: 1,
					},
					renderer: am5xy.AxisRendererX.new(root, {}),
				})
			);

			var sbValueAxis = scrollbar.chart.yAxes.push(am5xy.ValueAxis.new(root, {
					renderer: am5xy.AxisRendererY.new(root, {}),
				})
			);

			var sbSeries = scrollbar.chart.series.push(am5xy.LineSeries.new(root, {
					valueYField: "value",
					valueXField: "date",
					xAxis: sbDateAxis,
					yAxis: sbValueAxis,
				})
			);

			sbSeries.fills.template.setAll({
				visible: true,
				fill: am5.color(0xFF000),
				stroke: am5.color(0xFF000),
				fillOpacity: 1
			});' . "\n";
		}
		// END OF SCROLLBAR

		$html .=
			'var seriesSwitcher = am5stock.SeriesTypeControl.new(root, {
				stockChart: stockChart,
			});

			seriesSwitcher.events.on("selected", function (ev) {
				setSeriesType(ev.item.id);
			});

			function getNewSettings(series) {
				var newSettings = [];
				am5.array.each(
					["name", "valueYField", "highValueYField", "lowValueYField", "openValueYField", "calculateAggregates", "valueXField", "xAxis", "yAxis", "legendValueText", "stroke", "fill"],
					function (setting) {
						newSettings[setting] = series.get(setting);
					}
				);
				return newSettings;
			}

			function setSeriesType(seriesType) {        
				var currentSeries = stockChart.get("stockSeries");
				var newSettings = getNewSettings(currentSeries);
				
				var data = currentSeries.data.values;
				mainPanel.series.removeValue(currentSeries);
				
				var series;
				switch (seriesType) {
					case "line":
						series = mainPanel.series.push(am5xy.LineSeries.new(root, newSettings));
						break;
					case "candlestick":
					case "procandlestick":
						newSettings.clustered = false;
						series = mainPanel.series.push(am5xy.CandlestickSeries.new(root, newSettings));
						if (seriesType == "procandlestick") {
							series.columns.template.get("themeTags").push("pro");
						}
						break;
					case "ohlc":
						newSettings.clustered = false;
						series = mainPanel.series.push(am5xy.OHLCSeries.new(root, newSettings));
						break;
				}
				
				if (series) {
					valueLegend.data.removeValue(currentSeries);
					series.data.setAll(data);
					stockChart.set("stockSeries", series);
					var cursor = mainPanel.get("cursor");
					if (cursor) {
						cursor.set("snapToSeries", [series]);
					}
					valueLegend.data.insertIndex(0, series);
				}
			}
			
			var toolbar = am5stock.StockToolbar.new(root, {
				container: document.getElementById("chartcontrols"),
				stockChart: stockChart,
				controls: [' . "\n";
		if (esc_attr($atts['sp']) == 1) {
			$html .= 'am5stock.IndicatorControl.new(root, {
		      stockChart: stockChart,
		      legend: valueLegend
		    }),
		    am5stock.DateRangeSelector.new(root, {
		      stockChart: stockChart
		    }),
			am5stock.PeriodSelector.new(root, {
								stockChart: stockChart,
								periods: [{
									timeUnit: "day",
									count: 10,
									name: "10D",
									active: true,
									visible: false
								},
								{
									timeUnit: "month",
									count: 1,
									name: "1M"
								},
								{
									timeUnit: "month",
									count: 3,
									name: "3M"
								},
								{
									timeUnit: "month",
									count: 6,
									name: "6M"
								},
								{
									timeUnit: "year",
									count: 1,
									name: "1Y"
								},
								{
									timeUnit: "year",
									count: 5,
									name: "5Y"
								},
								{
									timeUnit: "max",
									name: "ALL"
								}]
							}), ' . "\n";
		}
		$html .= 'am5stock.SettingsControl.new(root, {
						stockChart: stockChart,
					}),
				],
			});


			
			

			var data = chartData;

			valueSeries.data.setAll(data);
			volumeSeries.data.setAll(data);

			' . "\n";

		if (esc_attr($atts['ssb']) == 1) {
			$html .= 'sbSeries.data.setAll(data);' . "\n";
		}


		$html .= '</script>' . "\n";

		// print_r($html);
		// exit();
		return $html;
	}
	
	




	
	/**
	*********************************************************************************
	 * Define the Chart  plugin shortcode
	 * @since  0.1.0
	 * @return string
	*********************************************************************************
	 */
	public function Chart_shortcode( $atts ) {
		$html = '';
		$dollarsign='';
		$atts = shortcode_atts(
			array(
				'bizid' => '',
				'api' => '',
				'sdiv' => 'chartdiv',
				'h' => 'https',
				's' => '',
				'sd' => '',
				'e' => '',
				'ds' => '1',
				'lang' => '1',
				'c' => '003399',
				'lc' => '666666',
				'ls' => '15',
				'lc2' => '666666',
				'ls2' => '11',
				'lc3' => '333333',
				'ls3' => '20',
				'la3' => '0.2',
				'cc' => '003399',
				'bgc' => 'ffffff',
				'bga' => '1.0',
				'cbc' => '999999',
				'cba' => '0.5',
				'bs' => '13',
				'bc' => '003399',
				'p' => '3m',
				'height' => '500px',
				'width' => '96%',
				'sp' => '1',
				'sc' => '1',
				'ssb' => '1',
				'sv' => '1',
				'lm' => '13',
				'rm' => '5',
				'tm' => '5',
				'bm' => '5',
				'vic' => '1',
				'ct' => 'line',
				'vls' => 'left',
				'vct' => 'column',
				'vfa' => '.9',
				'fa' => '0.2',
				'ca' => '0.1',
				'stl' => '1',
				'sil' => '1',
				'lt' => '2',
				'ps' => 'bottom',
				'centsep' => '.',
				'thoussep' => ',',
				'volumetext' => 'Volume',
				'pricetext' => 'Price',
				'ga' => '.2',
				'gc' => 'bbbbbb',
				'udg' => '0',
				'mode' => '0',
				'class' => '',
				'ulc' => '4CAF50',
				'ufc' => '4CAF50',
				'dlc' => 'ff0000',
				'dfc' => 'ff0000'
			),
			$atts
		);


switch ($atts['ds']) {
  case '0':
	$dollarsign='';
	break;
  case '1':
	$dollarsign='$';
	break;
  case '2':
	$dollarsign='\u20AC';
	break;
  case '3':
	$dollarsign='\u00A3';
	break;
  case '4':
	$dollarsign='\u20AA';
	break;
}


$bizid = $this->business_id;
$api = $this->key;
$ticker = $this->ticker;

if(esc_attr( $atts['bizid'])!=''){
	$bizid = esc_attr( $atts['bizid']);
}

if(esc_attr( $atts['api'])!=''){
	$api = esc_attr( $atts['api']);
}

if(esc_attr( $atts['s'])!=''){
	$ticker = esc_attr( $atts['s']);
}


if($atts['ct']=='candlestick' || $atts['ct']=='ohlc') $atts['mode']="1";

$html .= '<div id="' . esc_attr( $atts['sdiv'] ) . '" class="B2iChartContainer ' . $atts['class'] . '" style="height: ' . esc_attr( $atts['height'] ) . '; width:' . esc_attr( $atts['width'] ) . ';"></div>' . "\n";
//$html .= '<script src="' . esc_url(esc_attr( $atts['h'] ) . $this->base_url3 . 'profiles/inc/amstockgetJsData.asp?b=' ) . $this->business_id . '&s=' . esc_attr( $atts['s'] ) . '" type="text/javascript"></script>' . "\n";

// Base URL to your plugin folder
$jsbase_url = plugin_dir_url( __FILE__ );
$jsurl = esc_url( $jsbase_url . 'js/am3/');
$html .= '<script src="' . $jsurl . 'amcharts.js" type="text/javascript"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'serial.js" type="text/javascript"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'amstock.js" type="text/javascript"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'export.js" type="text/javascript"></script>' . "\n";
$html .= '<style src="https://stockcharting.s3.amazonaws.com/export.css" type="text/css"></style>' . "\n";
$html .= '<style src="https://stockcharting.s3.amazonaws.com/style.css" type="text/css"></style>' . "\n";
$html .= '<style src="https://www.b2i.us/profiles/inc/CSS.asp?m=5&b=' . $bizid . '&api=' . $api . '" type="text/javascript"></style>' . "\n";
$html .= '<script src="https://www.b2i.us/profiles/inc/amstockgetJsData2.asp?b=' . $bizid . '&api=' . $api . '&s=' . $ticker . '&m=' . esc_attr( $atts['mode'] ) . '" type="text/javascript"></script>' . "\n";
$html .= '<script type="text/javascript">' . "\n";
$html .= 'var chartData = [];' . "\n";
$html .= 'loadData();' . "\n";

if ($atts['lang']==2) {
$html .= 'AmCharts.monthNames = ["Eenero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];' . "\n";
$html .= 'AmCharts.shortMontNames = ["Een","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic"];' . "\n";
}




$html .= 'var chart = AmCharts.makeChart("' . esc_attr( $atts['sdiv'] ) . '", {type: "stock", addClassNames: true, ' . "\n";

$html .= ' pathToImages: "' . esc_url(esc_attr( $atts['h'] ) . $this->base_url3 . 'profiles/investor/ChartJS/images/') .'", dataSets: [{color: "#' . $atts['c'] . '",' . "\n";
$html .= 'title:"' . $atts['sd'] . '",' . "\n";
$html .= 'fieldMappings: [{' . "\n";
$html .= 'fromField: "price",' . "\n";
$html .= 'toField: "price"' . "\n";
$html .= '}, {' . "\n";
$html .= 'fromField: "open",' . "\n";
$html .= 'toField: "open"' . "\n";
$html .= '}, {' . "\n";
$html .= 'fromField: "high",' . "\n";
$html .= 'toField: "high"' . "\n";
$html .= '}, {' . "\n";
$html .= 'fromField: "low",' . "\n";
$html .= 'toField: "low"' . "\n";
$html .= '}, {' . "\n";
$html .= 'fromField: "volume",' . "\n";
$html .= 'toField: "volume"' . "\n";
$html .= '}, {' . "\n";
$html .= 'fromField: "svolume",' . "\n";
$html .= 'toField: "svolume"' . "\n";
$html .= '}, {' . "\n";
$html .= 'fromField: "svolume",' . "\n";
$html .= 'toField: "svolume"' . "\n";
$html .= '}],' . "\n";
$html .= ' dataProvider: chartData, categoryField: "date"' . "\n";
$html .= '}],' . "\n";
$html .= 'panels: [{' . "\n";
if($atts['sil']=="1"){
$html .= '"titles":[{"text":"' . $atts['e'] . ' ' . $atts['sd'] . '","size":' . $atts['ls3'] . ',"alpha":' . $atts['la3'] . ',"color":"#' . $atts['lc3'] . '"}],' . "\n";
}
$html .= 'thousandsSeparator:"' . $atts['thoussep'] . '", decimalSeparator:"' . $atts['centsep'] . '", showCategoryAxis: true,' . "\n";
$html .= 'title: "' . $atts['pricetext'] . '",' . "\n";
$html .= 'percentHeight: 75,' . "\n";

$html .= 'valueAxes:[{' . "\n";
$html .= 'gridAlpha: "' . $atts['ga'] . '"' . "\n";

if($atts['udg']=="1"){
$html .= ',"dashLength":5' . "\n";
}
$html .= '}],' . "\n";
$html .= 'categoryAxis:{' . "\n";
$html .= 'gridAlpha: "' . $atts['ga'] . '",' . "\n";
if($atts['udg']=="1"){
$html .= '"dashLength":5,' . "\n";
}
$html .= '"parseDates":true },' . "\n";
$html .= 'stockGraphs: [{' . "\n";
$html .= 'id: "g1",' . "\n";
$html .= 'type: "' . $atts['ct'] . '",' . "\n";
$html .= 'lineThickness: ' . $atts['lt'] . ',' . "\n";
$html .= 'openField: "open",' . "\n";
$html .= 'closeField: "price",' . "\n";
$html .= 'highField: "high",' . "\n";
$html .= 'lowField: "low",' . "\n";
$html .= 'volumeField: "volume",' . "\n";
$html .= 'valueField: "price",' . "\n";
$html .= 'fillAlphas: ' . $atts['fa'] . ',' . "\n";

$html .= 'lineColor:  "#' . $atts['ulc'] . '",' . "\n";
$html .= 'fillColors: "#' . $atts['ufc'] . '",' . "\n";
$html .= 'negativeLineColor: "#' . $atts['dlc'] . '",' . "\n";
$html .= 'negativeFillColors: "#' . $atts['dfc'] . '",' . "\n";
if($atts['mode']=="1"){
	$html .= 'useDataSetColors: false,' . "\n";
}

if($atts['mode']=="0"){
	$html .= 'balloonText:"<div style=\"\"><div style=\"text-align:center;font-weight:bold;padding:5px 5px;\">[[category]]</div></div><div style=\"display:table-row;\"><div class=\"chartballon chartstockpricedate\" style=\"display:table-cell;padding:2px;font-size:90%;\">Close</div><div class=\"chartballon chartstockprice\" style=\"display:table-cell;text-align:right;font-weight:bold;padding:2px;\">' . $dollarsign . '[[value]]</div></div>"';
}else{
	$html .= 'balloonText:"<div><div style=\"font-weight:bold;padding:3px 5px;\">[[category]]</div></div><div style=\"display:table-row;\"><div style=\"display:table-cell;padding:3px 5px;\">Open: </div><div style=\"display:table-cell;padding:3px 5px;text-align:right;\">' . $dollarsign . '[[open]]</div></div><div style=\"display:table-row;\"><div style=\"display:table-cell;padding:3px 5px;\">Close: </div><div style=\"display:table-cell;padding:3px 5px;text-align:right;\">' . $dollarsign . '[[close]] </div></div><div style=\"display:table-row;\"><div style=\"display:table-cell;padding:3px 5px;\">Low:</div><div style=\"display:table-cell;padding:3px 5px;text-align:right;\">' . $dollarsign . '[[low]] </div></div><div style=\"display:table-row;\"><div style=\"display:table-cell;padding:3px 5px;\">High:</div><div style=\"display:table-cell;padding:3px 5px;text-align:right;\">' . $dollarsign . '[[high]]</div></div><div style=\"display:table-row;\"><div style=\"display:table-cell;padding:3px 5px;\">Volume:</div><div style=\"display:table-cell;padding:3px 5px;text-align:right;\">[[svolume]]</div></div>"' . "\n";
}

$html .= '}]' . "\n";
if($atts['stl']=='1'){
$html .= ',stockLegend: {' . "\n";
$html .= 'fontSize: ' . $atts['bs'] . ',' . "\n";
$html .= 'valueTextRegular: " ' . $dollarsign . '[[value]]",' . "\n";
$html .= 'markerType: "none"' . "\n";
$html .= '}' . "\n";
}
$html .= '}' . "\n";

if ($atts['sv']==1) {
$html .= ',{' . "\n";
$html .= 'thousandsSeparator:"' . $atts['thoussep'] . '", decimalSeparator:"' . $atts['centsep'] . '",showCategoryAxis: false,' . "\n";
$html .= 'title: "' . $atts['volumetext'] . '",' . "\n";
$html .= 'percentHeight: 25,' . "\n";

$html .= 'valueAxes:[{' . "\n";
$html .= 'gridAlpha: "' . $atts['ga'] . '"' . "\n";
	if($atts['udg']=="1"){
$html .= ',"dashLength":5' . "\n";
	}
$html .= '}],' . "\n";

$html .= 'categoryAxis:{' . "\n";
$html .= 'gridAlpha: "' . $atts['ga'] . '"' . "\n";
	if($atts['udg']=="1"){
$html .= ',"dashLength":5,' . "\n";
	}
$html .= '},' . "\n";

$html .= 'stockGraphs: [{' . "\n";
$html .= 'valueField: "volume",' . "\n";
$html .= 'type: "' . $atts['vct'] . '",' . "\n";
$html .= 'cornerRadiusTop: 1,' . "\n";
$html .= 'fillAlphas: ' . $atts['vfa'] . ',' . "\n";
$html .= 'balloonText:"<span style=\"line-height:20px;\">Date: <b>[[category]]</b><br>' . $atts['volumetext'] . ': <b>[[value]]</b></span>"' . "\n";
$html .= '}],' . "\n";
$html .= 'stockLegend: {' . "\n";
$html .= 'valueTextRegular: " [[value]]",' . "\n";
$html .= 'markerType: "none"' . "\n";
$html .= '}' . "\n";
$html .= '}' . "\n";
}

$html .= '],' . "\n";
$html .= 'chartCursorSettings: {' . "\n";
if ($atts['sc']==0) {
$html .= 'enabled:false,' . "\n";
}else{
$html .= 'valueBalloonsEnabled: true,' . "\n";
$html .= 'fullWidth: true,' . "\n";
$html .= 'cursorAlpha: ' . $atts['ca'] . ',' . "\n";
$html .= 'cursorColor: "#' . $atts['cc'] . '",' . "\n";
$html .= 'graphBulletSize: 1,' . "\n";
$html .= 'valueLineBalloonEnabled:true,' . "\n";
$html .= 'valueLineEnabled:true,' . "\n";
$html .= 'valueLineAlpha: 0.5' . "\n";
}
$html .= '},' . "\n";
$html .= 'legendSettings: {' . "\n";
$html .= 'color: "#' . $atts['lc'] . '"' . "\n";
$html .= ',fontSize:' . $atts['ls'] . '' . "\n";
$html .= '},' . "\n";
$html .= 'balloon: {' . "\n";
$html .= 'maxWidth:320,' . "\n";
$html .= 'horizontalPadding: 5,' . "\n";
$html .= 'verticalPadding: 3,' . "\n";
$html .= 'fontSize: 13,' . "\n";
$html .= 'textAlign:"left",' . "\n";
$html .= 'borderThickness:1,' . "\n";
$html .= 'borderColor: "#' . $atts['bc'] . '",' . "\n";
$html .= 'borderAlpha:0.8,' . "\n";
$html .= 'fillColor: "#FFFFFF",' . "\n";
$html .= 'cornerRadius: 3,' . "\n";
$html .= 'fillAlpha:1,' . "\n";
$html .= 'offsetX: 105,' . "\n";
$html .= 'offsetY: 105' . "\n";
$html .= '},' . "\n";

if ($atts['sp']=='1') {
$html .= 'periodSelector: {' . "\n";
$html .= 'inputFieldsEnabled: false,' . "\n";
$html .= 'position: "' . $atts['ps'] . '",' . "\n";
$html .= 'dateFormat: "MM-DD-YYYY",' . "\n";
$html .= 'periods: [{' . "\n";
$html .= 'period: "DD",' . "\n";
	if ( esc_attr( $atts['p'] )=='10d' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 10,' . "\n";
$html .= 'label: "10 D"' . "\n";
$html .= '}, {' . "\n";
$html .= 'period: "MM",' . "\n";
	if ( esc_attr( $atts['p'] )=='1m' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 1,' . "\n";
$html .= 'label: "1 M"' . "\n";
$html .= '}, {' . "\n";
$html .= 'period: "MM",' . "\n";
	if ( esc_attr( $atts['p'] )=='3m' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 3,' . "\n";
$html .= 'label: "3 M"' . "\n";
$html .= '}, {' . "\n";
$html .= 'period: "MM",' . "\n";
	if ( esc_attr( $atts['p'] )=='6m' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 6,' . "\n";
$html .= 'label: "6 M"' . "\n";
$html .= '}, {' . "\n";
$html .= 'period: "YYYY",' . "\n";
	if ( esc_attr( $atts['p'] )=='1y' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 1,' . "\n";
$html .= 'label: "1 Y"' . "\n";
$html .= '}, {' . "\n"; 
$html .= 'period: "YYYY",' . "\n";
	if ( esc_attr( $atts['p'] )=='1y' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 5,' . "\n";
$html .= 'label: "5 Y"' . "\n";
$html .= '}, {' . "\n";
$html .= 'period: "MAX",' . "\n";
	if ( esc_attr( $atts['p'] )=='max' ) $html .= 'selected: true,' . "\n";
$html .= 'label: "ALL"' . "\n";
$html .= '}]' . "\n";
$html .= '},' . "\n";
}
$html .= 'panelsSettings: {' . "\n";
$html .= 'usePrefixes: true,' . "\n";
$html .= 'color: "#' . $atts['lc2'] . '",' . "\n";
$html .= 'fontSize: "' . $atts['ls2'] . '",' . "\n";
$html .= 'plotAreaFillColors: "#' . $atts['bgc'] . '",' . "\n";
$html .= 'plotAreaFillAlphas: ' . $atts['bga'] . ',' . "\n";
$html .= 'plotAreaBorderColor: "#' . $atts['cbc'] . '",' . "\n";
$html .= 'plotAreaBorderAlpha: ' . $atts['cba'] . ',' . "\n";
$html .= 'marginLeft:' . $atts['lm'] . ',' . "\n";
$html .= 'marginRight:' . $atts['rm'] . ',' . "\n";
$html .= 'marginTop:' . $atts['tm'] . ',' . "\n";
$html .= 'marginBottom:' . $atts['bm'] . ',' . "\n";
$html .= 'creditsPosition: "top-left"' . "\n";
$html .= ',addClassNames: true' . "\n";
$html .= '},' . "\n";
$html .= 'valueAxesSettings: {' . "\n";
$html .= 'gridColor: "#' . $atts['gc'] . '",' . "\n";
$html .= 'position: "' . $atts['vls'] . '"' . "\n";
if ($atts['vic']==0) {
$html .= ',inside: false' . "\n";
}
$html .= '},' . "\n";
$html .= 'chartScrollbarSettings: {' . "\n";
$html .= 'graph:"g1"' . "\n";
$html .= ',color: "#000000"' . "\n";
$html .= ',selectedBackgroundColor: "#' . $atts['bgc'] . '"' . "\n";
$html .= ',selectedGraphFillColor: "#' . $atts['c'] . '"' . "\n";
$html .= ',selectedGraphLineColor: "#' . $atts['c'] . '"' . "\n";
$html .= ',selectedGraphFillAlpha: .1' . "\n";
$html .= ',selectedGraphLineAlpha: 1' . "\n";
$html .= ',graphFillColor: "#e7e7e7"' . "\n";
$html .= ',backgroundColor: "#' . $atts['bgc'] . '"' . "\n";
$html .= ',graphType: "line"' . "\n";
if ($atts['ssb']==0) {
$html .= ',enabled:false' . "\n";
}
$html .= '},' . "\n";
$html .= 'categoryAxesSettings: {' . "\n";
$html .= 'gridColor: "#' . $atts['gc'] . '",' . "\n";
$html .= 'startOnAxis: true,' . "\n";
$html .= 'equalSpacing: true,' . "\n";

if($atts['mode']=="1"){ $html .= ' maxSeries:15000' . "\n"; }
$html .= '}' . "\n";
$html .= '});' . "\n";


$html .= '</script>' . "\n";
	return $html;
}



	/**
	*********************************************************************************
	 * Define the Intraday Chart  plugin shortcode
	 * @since  0.1.0
	 * @return string
	*********************************************************************************
	 */
	public function intradaychart_shortcode( $atts ) {
		$html = '';
		$dollarsign='';
		$atts = shortcode_atts(
			array(
				'bizid' => '',
				'api' => '',
				'sdiv' => 'ichartdiv',
				'h' => 'https',
				's' => '',
				'sd' => '',
				'e' => '',
				'ds' => '1',
				'lang' => '1',
				'c' => '003399',
				'lc' => '666666',
				'ls' => '15',
				'lc2' => '666666',
				'ls2' => '11',
				'lc3' => '333333',
				'ls3' => '20',
				'la3' => '0.2',
				'cc' => '003399',
				'bgc' => 'ffffff',
				'bga' => '1.0',
				'cbc' => '999999',
				'cba' => '0.5',
				'bs' => '13',
				'bc' => '003399',
				'p' => '3m',
				'height' => '500px',
				'width' => '96%',
				'sp' => '1',
				'sc' => '1',
				'ssb' => '1',
				'sv' => '0',
				'lm' => '13',
				'rm' => '5',
				'tm' => '5',
				'bm' => '5',
				'vic' => '1',
				'ct' => 'line',
				'vls' => 'left',
				'vct' => 'column',
				'vfa' => '.9',
				'fa' => '0.2',
				'ca' => '0.1',
				'stl' => '1',
				'sil' => '1',
				'lt' => '2',
				'ps' => 'bottom',
				'centsep' => '.',
				'thoussep' => ',',
				'volumetext' => 'Volume',
				'pricetext' => 'Price',
				'ga' => '.2',
				'gc' => 'bbbbbb',
				'class' => '',
				'udg' => '0'
			),
			$atts
		);

switch ($atts['ds']) {
  case '0':
	$dollarsign='';
	break;
  case '1':
	$dollarsign='$';
	break;
  case '2':
	$dollarsign='\u20AC';
	break;
  case '3':
	$dollarsign='\u00A3';
	break;
  case '4':
	$dollarsign='\u20AA';
	break;
}

$bizid = $this->business_id;
$api = $this->key;
$ticker = $this->ticker;

if($atts['sd']=='')
	$atts['sd'] = $ticker;


if(esc_attr( $atts['bizid'])!=''){
	$bizid = esc_attr( $atts['bizid']);
}

if(esc_attr( $atts['api'])!=''){
	$api = esc_attr( $atts['api']);
}

if(esc_attr( $atts['s'])!=''){
	$ticker = esc_attr( $atts['s']);
}

// Base URL to your plugin folder
$jsbase_url = plugin_dir_url( __FILE__ );
$jsurl = esc_url( $jsbase_url . 'js/am3/');

$html .= '<div id="' . esc_attr( $atts['sdiv'] ) . '" class="B2iIntraChartContainer ' . $atts['class'] . '" style="height: ' . esc_attr( $atts['height'] ) . '; width:' . esc_attr( $atts['width'] ) . ';"></div>' . "\n";
$html .= '<script src="' . $jsurl . 'amcharts.js" type="text/javascript"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'serial.js" type="text/javascript"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'amstock.js" type="text/javascript"></script>' . "\n";
$html .= '<script src="' . $jsurl . 'export.js" type="text/javascript"></script>' . "\n";
$html .= '<style src="https://stockcharting.s3.amazonaws.com/export.css" type="text/css"></style>' . "\n";
$html .= '<style src="https://stockcharting.s3.amazonaws.com/style.css" type="text/css"></style>' . "\n";
$html .= '<style src="https://www.b2i.us/profiles/inc/CSS.asp?m=5&b=' . $bizid . '&api=' . $api . '" type="text/javascript"></style>' . "\n";
$html .= '<script src="https://www.b2i.us/profiles/inc/amstockIntraGetJsData.asp?b=' . $bizid . '&api=' . $api . '&s=' . $ticker . '" type="text/javascript"></script>' . "\n";
$html .= '<script type="text/javascript">' . "\n";
$html .= 'var chartIntraData = [];' . "\n";
$html .= 'loadIntraData();' . "\n";

$html .= 'var ichart = AmCharts.makeChart("' . esc_attr( $atts['sdiv'] ) . '", {type: "stock", addClassNames: true, "responsive":{"enabled": true },' . "\n";

$html .= ' pathToImages: "' . esc_url(esc_attr( $atts['h'] ) . $this->base_url3 . 'profiles/investor/ChartJS/images/') .'", ';
$html .= ' dataSets: [{color: "#' . $atts['c'] . '",' . "\n";
$html .= 'title:"' . $atts['sd'] . '",' . "\n";
$html .= 'fieldMappings: [{"fromField": "price", "toField": "price"}],' . "\n";
$html .= ' dataProvider: chartIntraData, categoryField: "date", "dataformat":"mm"' . "\n";
$html .= '}],' . "\n";


$html .= 'panels: [{ "showCategoryAxis": true,' . "\n";

if($atts['sil']=="1"){
$html .= '"titles":[{"text":"' . $atts['e'] . ' ' . $atts['sd'] . '","size":' . $atts['ls3'] . ',"alpha":' . $atts['la3'] . ',"color":"#' . $atts['lc3'] . '"}],' . "\n";
}

$html .= 'thousandsSeparator:"' . $atts['thoussep'] . '", decimalSeparator:"' . $atts['centsep'] . '",' . "\n";
$html .= 'title: "' . $atts['pricetext'] . '",' . "\n";
$html .= 'percentHeight: 100,' . "\n";

$html .= 'valueAxes:[{' . "\n";
$html .= '"id": "v1", gridAlpha: "' . $atts['ga'] . '", parseDates: true' . "\n";
if($atts['udg']=="1"){
$html .= ',"dashLength":5' . "\n";
}
$html .= '}],' . "\n";

$html .= 'categoryAxis:{' . "\n";
$html .= 'gridAlpha: "' . $atts['ga'] . '",' . "\n";
if($atts['udg']=="1"){
$html .= '"dashLength":5,' . "\n";
}
$html .= '"parseDates":true,"dateFormats":[{"period":"mm","format":"LL:NN"},{"period":"hh","format":"LL:NN"}] },' . "\n";

$html .= 'stockGraphs: [{' . "\n";
$html .= 'id: "g1",' . "\n";
$html .= 'type: "' . $atts['ct'] . '",' . "\n";
$html .= 'lineThickness: ' . $atts['lt'] . ',' . "\n";
$html .= 'valueField: "price",' . "\n";
$html .= 'fillAlphas: ' . $atts['fa'] . ',' . "\n";
$html .= 'balloonText:"<div class=\"stockchartballoon\" style=\"display:table-row;\"><div style=\"display:table-cell;text-align:right;font-weight:bold;padding:3px;\">Time:</div><div style=\"display:table-cell;text-align:left;font-weight:bold;padding:3px;\">[[category]]</div></div>';
$html .= '<div style=\"display:table-row;\"><div class=\"chartballon chartstockpricedate\" style=\"display:table-cell;padding:3px;font-size:90%;\">Close</div><div class=\"chartballon chartstockprice\" style=\"display:table-cell;text-align:right;font-weight:bold;padding:3px;\">' . $dollarsign . '[[value]]</div></div>"';
$html .= '}]' . "\n";

if($atts['stl']=='1'){
$html .= ',stockLegend: {' . "\n";
$html .= 'fontSize: ' . $atts['bs'] . ',' . "\n";
$html .= 'valueTextRegular: " ' . $dollarsign . '[[value]]",' . "\n";
$html .= 'markerType: "none", "valueAlign":"left"' . "\n";
$html .= '}' . "\n";
}
$html .= '}' . "\n";


$html .= '],' . "\n";
$html .= 'chartCursorSettings: {' . "\n";
if ($atts['sc']==0) {
$html .= 'enabled:false,' . "\n";
}else{
$html .= 'valueBalloonsEnabled: true,' . "\n";
$html .= 'fullWidth: true,' . "\n";
$html .= 'cursorAlpha: ' . $atts['ca'] . ',' . "\n";
$html .= 'cursorColor: "#' . $atts['cc'] . '",' . "\n";
$html .= 'graphBulletSize: 1,' . "\n";
$html .= 'valueLineBalloonEnabled:true,' . "\n";
$html .= 'valueLineEnabled:true,' . "\n";
$html .= 'valueLineAlpha: 0.7,' . "\n";
$html .= 'leaveAfterTouch:true,' . "\n";
$html .= '"minPeriod": "mm",' . "\n";
$html .= '"cursorPosition":"mouse",' . "\n";
$html .= '"categoryBalloonDateFormats" :[{"period":"ss","format":"LL:NN:SS"},{"period":"mm","format":"LL:NN"},{"period":"hh","format":"LL:NN"}]' . "\n";
}
$html .= '},' . "\n";

$html .= '"chartCursor": { "categoryBalloonDateFormat":"LL:NN" },' . "\n";

$html .= 'legendSettings: {' . "\n";
$html .= 'color: "#' . $atts['lc'] . '"' . "\n";
$html .= ',fontSize:' . $atts['ls'] . '' . "\n";
$html .= '},' . "\n";
$html .= 'balloon: {' . "\n";
$html .= 'maxWidth:320,' . "\n";
$html .= 'horizontalPadding: 5,' . "\n";
$html .= 'verticalPadding: 3,' . "\n";
$html .= 'fontSize: 13,' . "\n";
$html .= 'textAlign:"left",' . "\n";
$html .= 'borderThickness:1,' . "\n";
$html .= 'borderColor: "#' . $atts['bc'] . '",' . "\n";
$html .= 'borderAlpha:0.8,' . "\n";
$html .= 'fillColor: "#FFFFFF",' . "\n";
$html .= 'cornerRadius: 3,' . "\n";
$html .= 'fillAlpha:1,' . "\n";
$html .= 'offsetY: 25, "animationDuration":0.2' . "\n";
$html .= '},' . "\n";

if ($atts['sp']=='1') {
$html .= 'periodSelector: {' . "\n";
$html .= 'inputFieldsEnabled: false,' . "\n";
$html .= 'position: "' . $atts['ps'] . '",' . "\n";
$html .= 'dateFormat: "YYYY-MM-DD JJ:NN",' . "\n";
$html .= 'periods: [{' . "\n";
$html .= 'period: "mm",' . "\n";
	if ( esc_attr( $atts['p'] )=='1h' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 60,' . "\n";
$html .= 'label: "1 h"' . "\n";
$html .= '}, {' . "\n";
$html .= 'period: "mm",' . "\n";
	if ( esc_attr( $atts['p'] )=='2h' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 120,' . "\n";
$html .= 'label: "2 h"' . "\n";
$html .= '}, {' . "\n";
$html .= 'period: "mm",' . "\n";
	if ( esc_attr( $atts['p'] )=='4h' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 240,' . "\n";
$html .= 'label: "4 h"' . "\n";
$html .= '}, {' . "\n";
$html .= 'period: "mm",' . "\n";
	if ( esc_attr( $atts['p'] )=='6h' ) $html .= 'selected: true,' . "\n";
$html .= 'count: 360,' . "\n";
$html .= 'label: "6 h"' . "\n";
$html .= '}, {' . "\n";
$html .= 'period: "MAX",' . "\n";
	if ( esc_attr( $atts['p'] )=='max' ) $html .= 'selected: true,' . "\n";
$html .= 'label: "ALL"' . "\n";
$html .= '}]' . "\n";
$html .= '},' . "\n";
}
$html .= 'panelsSettings: {' . "\n";
$html .= 'usePrefixes: true,' . "\n";
$html .= 'color: "#' . $atts['lc2'] . '",' . "\n";
$html .= 'fontSize: "' . $atts['ls2'] . '",' . "\n";
$html .= 'plotAreaFillColors: "#' . $atts['bgc'] . '",' . "\n";
$html .= 'plotAreaFillAlphas: ' . $atts['bga'] . ',' . "\n";
$html .= 'plotAreaBorderColor: "#' . $atts['cbc'] . '",' . "\n";
$html .= 'plotAreaBorderAlpha: ' . $atts['cba'] . ',' . "\n";
$html .= 'marginLeft:' . $atts['lm'] . ',' . "\n";
$html .= 'marginRight:' . $atts['rm'] . ',' . "\n";
$html .= 'marginTop:' . $atts['tm'] . ',' . "\n";
$html .= 'marginBottom:' . $atts['bm'] . ',' . "\n";
$html .= 'creditsPosition: "top-left"' . "\n";
$html .= ',addClassNames: true' . "\n";
$html .= '},' . "\n";
$html .= 'valueAxesSettings: {' . "\n";
$html .= 'gridColor: "#' . $atts['gc'] . '",' . "\n";
$html .= 'position: "' . $atts['vls'] . '"' . "\n";
if ($atts['vic']==0) {
$html .= ',inside: false' . "\n";
}
$html .= '},' . "\n";
$html .= 'chartScrollbarSettings: {' . "\n";
$html .= 'graph:"g1"' . "\n";
$html .= ',selectedBackgroundColor: "#' . $atts['bgc'] . '"' . "\n";
$html .= ',selectedGraphFillColor: "#' . $atts['c'] . '"' . "\n";
$html .= ',selectedGraphLineColor: "#' . $atts['c'] . '"' . "\n";
$html .= ',selectedGraphFillAlpha: .1' . "\n";
$html .= ',selectedGraphLineAlpha: 1' . "\n";
$html .= ',graphFillColor: "#e7e7e7"' . "\n";
$html .= ',backgroundColor: "#' . $atts['bgc'] . '"' . "\n";
if ($atts['ssb']==0) {
$html .= ',enabled:false' . "\n";
}
$html .= '},' . "\n";

$html .= 'categoryAxesSettings: {"minPeriod": "mm", gridColor: "#' . $atts['gc'] . '",startOnAxis: true, equalSpacing: true, "parseDates":true' . "\n";
$html .= '}' . "\n";

$html .= '});' . "\n";

$html .= '</script>' . "\n";

return $html;
}



	/**
	*********************************************************************************
	 */
	protected function remove_empty_url_args( $arg ) {
		return ( $arg !== '' );
	}
}
