Php canvasjs indexLabel not displaying

Hello everyone, my code shows that I can add an array element to one set but not another and I am not seeing where is the problem.
I am using the nulls as a separator in my graph.


	//---------------------------------------------------------------------------------------
	//	reindex array and insert nulls
	//---------------------------------------------------------------------------------------
				
		// $i	element counter
		// $j	array sequence counter
		// $k	differentiates array sets
		
		// first time only - insert new elements into array to correct positioning of array
		if ($i = 1) {

			// Set the index number from three
			$New_start_index = 1;		// set index starting number or letter
			$arr = $data_array;			// copy data_array to new array
			
			// perform the reindex
			$arr = array_combine(range(2, count($arr) + (1)), array_values($arr));
			
			// copy reindexed array to data_array
			$data_array = $arr;
			
			// insert new first record into data_array
			array_splice( $data_array, 0, 0, "null");
			
		} // end if
		
		// insert new null record throughout array
		foreach($data_array as $key => $value) {				
			if ($i % 7 == 0) {					
				array_splice( $data_array, $i, 0, "null");	// insert new elements at intervals
			}
			$i++; $j++;
			
		} // end foreach
		
		// append null to last record of array
		$y = "null"; array_splice($data_array, count($data_array), 0, array($y) );
		
		//$data_array_count = count($data_array); echo $data_array_count; echo "<br /><br />"."\n";
		//echo "<pre>"; print_r($data_array); echo "</pre>"; echo "<br /><br />"."\n";
		
	//---------------------------------------------------------------------------------------
	//	assign canvasjs indexLabel and append to array element where needed
	//---------------------------------------------------------------------------------------
		
/*	test data before nulls
5,8,21,32,35,3,
4,8,12,31,36,3,
6,13,16,20,45,3
*/
		
/*	test data after inserting nulls
null,5,8,21,32,35,3,
null,4,8,12,31,36,3,
null,6,13,16,20,45,3,
null
*/
		
	$i = 1; $j = 1; $k = 1;		// reset counters
	foreach($data_array as $key => $value) {
		 
		// display values before loop for debugging
		// displays array set, element counter, sequence, key, value
		//echo $k.", ".$i.", ".$j.", ".$key.", ".$value; echo "<br />"."\n";
		
		$j = $k;								// differentiates array sets			
		if ($i % 7 === 0) { $k = $k + 1; }		// increments k array set count			
		
		// use modulus to assign array elements to points		
		if ($i % 7 === 0) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
		if ($i % 7 === 1) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
		if ($i % 7 === 2) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
		if ($i % 7 === 3) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
		if ($i % 7 === 4) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
		if ($i % 7 === 5) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
	
		// this displays the extra assigned element indexLabel
		if ($i % 7 === 6) { 
		$points = array( "name" => $j, "x" => $key, "y" => $value, "indexLabel" => "displays" ); }
	
		// The problem is that it does not display the extra assigned element here
		// assign canvasjs indexLabel and append to array element
		if ($i % 7 === 7) { 
		$points = array( "name" => $j, "x" => $key, "y" => $value, "indexLabel" => "no display" ); }

		/*
		various attempts using alternate methods
		if ($i % 7 === 7) {
			//$points = array( "name" => $j, "x" => $key, "y" => $value );
			//$points = array( "name" => $j, "x" => $key, "y" => $value, "indexLabel" => "hit" );
			
			//$replacement = array( "name" => $j, "x" => $key, "y" => $value, "indexLabel" => "hit" );
			//$points = array_replace($points, $replacement);
		}
		*/
		
		array_push($dataPoints, $points);		// populate array set with separators			
		$i++;									// increase element counter
		
	} // end foreach
		
	echo '<h2 style="margin-left: 10px; text-align: left;">json_encode points for debugging: </h2>';
	echo json_encode($dataPoints); echo "<br /><br />";
	
	//$data_array_count = count($dataPoints); echo $data_array_count; echo "<br /><br />"."\n";
	//echo "<pre>"; print_r($dataPoints); echo "</pre>"; echo "<br /><br />"."\n";
	//echo "<pre>"; var_dump($dataPoints); echo "</pre>"; echo "<br />"."\n";
	
	//	Arithmetic Operators integer modulo		
	//	https://www.php.net/manual/en/language.operators.arithmetic.php
		
**output from the above**
json_encode points for debugging:
[{"name":1,"x":0,"y":"null"},{"name":1,"x":1,"y":5},{"name":1,"x":2,"y":8},{"name":1,"x":3,"y":21},{"name":1,"x":4,"y":32},{"name":1,"x":5,"y":35,"indexLabel":"displays"},{"name":1,"x":6,"y":3},{"name":2,"x":7,"y":"null"},{"name":2,"x":8,"y":4},{"name":2,"x":9,"y":8},{"name":2,"x":10,"y":12},{"name":2,"x":11,"y":31},{"name":2,"x":12,"y":36,"indexLabel":"displays"},{"name":2,"x":13,"y":3},{"name":3,"x":14,"y":"null"},{"name":3,"x":15,"y":6},{"name":3,"x":16,"y":13},{"name":3,"x":17,"y":16},{"name":3,"x":18,"y":20},{"name":3,"x":19,"y":45,"indexLabel":"displays"},{"name":3,"x":20,"y":3},{"name":4,"x":21,"y":"null"}]

You may have these things defined on your page but I found them missing on the code you posted. Also note the change to your IF condition.

/*
$data_array = array(5,8,21,32,35,3,
4,8,12,31,36,3,
6,13,16,20,45,3);
*/
$data_array = array(null,5,8,21,32,35,3,
null,4,8,12,31,36,3,
null,6,13,16,20,45,3,
null);
$dataPoints = array();
$i = 1;
$j = 1;
$k = 1;

	//---------------------------------------------------------------------------------------
	//	reindex array and insert nulls
	//---------------------------------------------------------------------------------------
				
		// $i	element counter
		// $j	array sequence counter
		// $k	differentiates array sets
		
		// first time only - insert new elements into array to correct positioning of array
		if ($i == 1) {
//etc

Returns:

[{"name":1,"x":0,"y":"null"},{"name":1,"x":1,"y":null},{"name":1,"x":2,"y":5},{"name":1,"x":3,"y":8},{"name":1,"x":4,"y":21},{"name":1,"x":5,"y":32,"indexLabel":"displays"},{"name":1,"x":6,"y":35},{"name":2,"x":7,"y":"null"},{"name":2,"x":8,"y":3},{"name":2,"x":9,"y":null},{"name":2,"x":10,"y":4},{"name":2,"x":11,"y":8},{"name":2,"x":12,"y":12,"indexLabel":"displays"},{"name":2,"x":13,"y":31},{"name":3,"x":14,"y":"null"},{"name":3,"x":15,"y":36},{"name":3,"x":16,"y":3},{"name":3,"x":17,"y":null},{"name":3,"x":18,"y":6},{"name":3,"x":19,"y":13,"indexLabel":"displays"},{"name":3,"x":20,"y":16},{"name":4,"x":21,"y":"null"},{"name":4,"x":22,"y":20},{"name":4,"x":23,"y":45},{"name":4,"x":24,"y":3},{"name":4,"x":25,"y":null},{"name":4,"x":26,"y":"null","indexLabel":"displays"}]

This will never happen.

if ($i % 7 === 7) {

Maybe you want $i greater than 7 and remainder 0 ??? Not exactly sure what you want.

if ($i > 7 && $i % 7 === 0) { 
		$points = array( "name" => $j, "x" => $key, "y" => $value, "indexLabel" => "no display" ); }

Hello Drummin,
I am uploading my file as it will make it easier.
Thanks for replying.
form-chart-numbers.html.php (16.7 KB)

Hmm well,
Line 94, fixed IF condition; if ($i == 1).

Dealing with natural array keys starting from zero I needed to set $i = 0; on line 143 so $i matched $key.

I assume using label “displays” for remainder of 6 was for testing. // Commented out.

Line 438 describes the root of the problem where if not a number “hides x description text and label formatting”…

So on line 143 I set y = 0 instead of "null" i.e. $value
I also set $k = 0 as the array set was off by 1 showing set 2,3,4

Note that IF condition you had at 167 would never be true so I changed it to
if ($i > 1 && $i % 7 === 0)

Your display text says “Point 0,0 is automatically inserted
between sets as a separator.” so I set the label to 0 0 for remainder of 7 === 0 .

Note, I tried to get 0 to display on the 0 line but it looks like all lines have that extra orange top displaying 1 line higher.

Anyway, that’s what I did to this version.
form-chart-numbers.html.php (16.9 KB)

Drummin, That works for me. Many thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.