Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional colors? #50

Closed
gamadoleo opened this issue Jun 14, 2021 · 24 comments
Closed

Conditional colors? #50

gamadoleo opened this issue Jun 14, 2021 · 24 comments

Comments

@gamadoleo
Copy link
Contributor

Is conditional color supported? tried to add directly via html <font color"red">
however without success, I would like to know what is the recommended way to work with conditional colors, for example I wanted to display a zebra listing with conditioned color.

@Rctnet
Copy link
Member

Rctnet commented Jun 17, 2021

use is_integer($F{rowIndex}/2) into printWhen property on band, create a two bands

@gamadoleo
Copy link
Contributor Author

I understand is because I don't have this rowIndex in the select, wouldn't it be more interesting to support this conditional no?:
($V{COLUMN_COUNT}% 2) == 0

@Rctnet
Copy link
Member

Rctnet commented Jun 17, 2021

You can add here

$obj->arrayVariable['REPORT_COUNT']["ans"] = $rowIndex;

@gamadoleo
Copy link
Contributor Author

As you guide requires two bands, I tried something simpler using a rectangle and applying the print condition on it, but it didn't work... are these conditionings also ready for rectangle?

@gamadoleo
Copy link
Contributor Author

but mind you, jasper report has style functionality and in a style we can add conditional formatting, where can i be testing and modifying to support conditional formatting of the style?

@Rctnet
Copy link
Member

Rctnet commented Jun 17, 2021

I understand now, but i´m sorry, i never use it, where in jasperstudio can be access it ?

@Rctnet
Copy link
Member

Rctnet commented Jun 17, 2021

I found styles, i think what a way to support this
in

$this->group_handler($ObjElement);

add a function to read each style and store into a global array, and all objects what you want to use styles, load style property into a object, (all objects are a separate Class) and use styles properties to decore that element, but yet no locate conditional styles, help me about this and i help you to think a way to do this.

@gamadoleo
Copy link
Contributor Author

thanks for the support, come on I'll demonstrate what I've achieved so far

@gamadoleo
Copy link
Contributor Author

gamadoleo commented Jun 17, 2021

in Report.php on line 68 I added the following debugging:

            //echo $className."<br/>";               
            if(ucfirst($obj)=='Style'){
            echo "<br/><br/>".$className."<br/>";
            print_r($value);
            echo "<br/><br/>";     
            if(isset($value->conditionalStyle)){
                echo "conditionExpression: ".$value->conditionalStyle->conditionExpression;
                echo "<br/>";                
                //get definition style condicional
                $style= $value->conditionalStyle->style->attributes();
                var_dump($style);   
                echo "<br/>";         
                echo "mode: ".$style['mode']."<br/>";
                echo "backcolor: ".$style['backcolor']."<br/><br/>";   
                
            }
            } 

e o result is:

JasperPHP\Style
SimpleXMLElement Object ( [@attributes] => Array ( [name] => Title [fontName] => Times New Roman [fontSize] => 50 [isBold] => true ) )



JasperPHP\Style
SimpleXMLElement Object ( [@attributes] => Array ( [name] => SubTitle [forecolor] => #736343 [fontName] => Arial [fontSize] => 18 ) )



JasperPHP\Style
SimpleXMLElement Object ( [@attributes] => Array ( [name] => Column header [forecolor] => #666666 [fontName] => Arial [fontSize] => 12 [isBold] => true ) )



JasperPHP\Style
SimpleXMLElement Object ( [@attributes] => Array ( [name] => Detail [fill] => Solid [scaleImage] => FillFrame [markup] => none [fontName] => Arial [fontSize] => 12 ) [conditionalStyle] => SimpleXMLElement Object ( [style] => SimpleXMLElement Object ( ) ) )

conditionExpression:
object(SimpleXMLElement)#9 (0) { }
mode:
backcolor:



JasperPHP\Style
SimpleXMLElement Object ( [@attributes] => Array ( [name] => Row [mode] => Transparent [backcolor] => #EDE8E8 [pattern] => ) [conditionalStyle] => SimpleXMLElement Object ( [conditionExpression] => $V{COLUMN_COUNT}%2 == 0 [style] => SimpleXMLElement Object ( [@attributes] => Array ( [mode] => Opaque [backcolor] => #F0F0F0 ) ) ) )

conditionExpression: $V{COLUMN_COUNT}%2 == 0
object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(2) { ["mode"]=> string(6) "Opaque" ["backcolor"]=> string(7) "#F0F0F0" } }
mode: Opaque
backcolor: #F0F0F0

@gamadoleo
Copy link
Contributor Author

however, for this to be possible, it is necessary to make an adjustment in the initialization of the xml, I see that I modified it, I would have to see if this affects your structure used so far, but this way it displays the conditional expression of the style:

$xml = simplexml_load_string($xmlFile,null,LIBXML_NOCDATA);

@gamadoleo
Copy link
Contributor Author

as in the definition of objects, for example, in TextField there is the property style with the name of the style, I believe it is possible to search the definition of the style and apply the conditional

@Rctnet
Copy link
Member

Rctnet commented Jun 17, 2021

i hope what the best way is it

// atribui o conteúdo do label

if(ucfirst($obj)=='Style'){
$this->addStyle($obj); // create a new method into Report.php
}
puclic function addStyle($style){
$this->arrayStyles[] = $style; // here you can trate all parameter of style
}

make this, for example in texfield you get the style property and this line you can get the report object

$obj = is_array($obj) ? $obj[0] : $obj;

you can get $obj->arrayStyles and use it to decorate a element

@gamadoleo
Copy link
Contributor Author

gamadoleo commented Jun 17, 2021

following your idea I created methods and I can apply exactly the style definitions, all the definitions: background, color and etc... this in TextFiel for now... now I need to see how to apply the conditional style...

in Report.php
add
public $arrayStyles;

add

          //echo $className."<br/>"; 
            // atribui o conteúdo do label
            if(ucfirst($obj)=='Style'){
            $this->addStyle($value); // create a new method into Report.php
            }    

new methods

public function addStyle($style){
//print_r($style);return;
$attributes = $style->attributes();
$key = $attributes['name'];
$this->arrayStyles["{$key}"] = $style; // here you can trate all parameter of style
}

public function getStyle($key){
    if(isset($this->arrayStyles["{$key}"])){
    return $this->arrayStyles["{$key}"];
    }
}
public function applyStyle($key, &$reportElement){
    $style = $this->getStyle($key);
    if($style){
        $attributes = $style->attributes();
        foreach($attributes as $key => $value){
            //ignore
            if(!in_array($key,array('name'))){
                //echo "{$key} - {$value}<br/>";    
                $reportElement[$key]=$value;                             
            }   
        }
    }

in TextField.php

` //print_r( $data["@attributes"]);

    //exist style
    if(isset($data->reportElement['style'])){
    $name = $data->reportElement['style'];
    $obj->applyStyle($name, $data->reportElement);
    }`

@gamadoleo
Copy link
Contributor Author

that is, now when applying the style, you first need to check the conditional expression to choose which definition to apply the default or the conditional style...

@Rctnet
Copy link
Member

Rctnet commented Jun 18, 2021

this code help you to load and execute expression, i think it is same as printWhenExpression

$print_expression_result = false;

@gamadoleo
Copy link
Contributor Author

get it here by modifying a little, so it checks and gets the style according to the conditional, it even accepts more than one conditional in the same style... I just can't say if this will affect the generator's performance... at the same here I haven't seen any effect, but everything can be improved.

@gamadoleo
Copy link
Contributor Author

gamadoleo commented Jun 18, 2021

modify for

`public function applyStyle($key, &$reportElement, $rowData){
        $style = $this->getStyle($key);
        if($style){
            //default
            $attributes = $style->attributes();
            if(isset($style->conditionalStyle)){ 
                //percore os styles
                foreach($style->conditionalStyle as $styleNew){                
                    $expression = $styleNew->conditionExpression;             
                    //echo $expression;
                    $resultExpression = false;
                    $expression = $this->get_expression($expression, $rowData);
                    //echo 'if(' . $expression . '){$resultExpression=true;}<br/>';
                    eval('if(' . $expression . '){$resultExpression=true;}'); 
                    //echo $resultExpression."<br/>";
                    if($resultExpression){
                        //get definition style condicional
                        $attributCondicional= $styleNew->style->attributes();
                        $attributes = $attributCondicional;
                        break;
                        //var_dump($attributCondicional);  
                    }      
                }
            }           
           //change propriedades 
            foreach($attributes as $key => $value){
                //ignore
                if(!in_array($key,array('name'))){
                    //echo "{$key} - {$value}<br/>";    
                    $reportElement[$key]=$value;                             
                }   
            }
           
        }        
    }`

@Rctnet
Copy link
Member

Rctnet commented Jun 18, 2021

great!!

@gamadoleo
Copy link
Contributor Author

about adding new variables I added in
Report.php
$obj->arrayVariable['COLUMN_COUNT'] = $rowIndex;//by gamadoleo

however to work the replace I had to make a small adjustment in the getValOfVariable method in the last one else

//by gamadoleo
if($ans==''){
return str_ireplace(array('$V{' . $variable . '}'), $val, $text);
}
return str_ireplace(array('$V{' . $variable . '}'), array($ans), $text);

@gamadoleo
Copy link
Contributor Author

the way I did the application of the style, it performs the conditional check on each TextField object, I don't know if I would be able to reduce this check, you who are more inside can analyze my function and see if you can improve the performance. but in the test here a 71 page report generates in less than 10s, but as I said everything can be improved.

@Rctnet
Copy link
Member

Rctnet commented Jun 18, 2021

ok, send a Pull Request with yours updates

@Rctnet
Copy link
Member

Rctnet commented Jun 18, 2021

add zebra style to cities sample

@gamadoleo
Copy link
Contributor Author

OK, just confirm about adding new variables for replication if I did something wrong? to not need to modify a getValOfVariable function

no details I added like this:
$obj->arrayVariable['COLUMN_COUNT'] = $rowIndex; // by gamma oil

however, tweak the getValOfVariable function

@gamadoleo
Copy link
Contributor Author

I will provide the example and send

ignore the aforementioned setting of Variable, actually the REPORT_COUNT works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants