Как нарисовать BarChart с MySQL данными в React native? - PullRequest
0 голосов
/ 01 марта 2020

Мне нужно нарисовать диаграмму с MySQL данными сервера.

Я нашел этот пример, но когда я пытаюсь поместить свои динамические данные c, он не работает:

import React from 'react'
import { BarChart, Grid } from 'react-native-svg-charts'
import * as shape from 'd3-shape'
import { BarChart, Grid } from 'react-native-svg-charts'

export default class FetchExample extends React.Component {

    //Function to recupere my data from my php file ///////////////////////////////////
    constructor(props){
        super(props);
        this.state ={ isLoading: true}
      }

      componentDidMount(){
        return fetch('https://127.168.1.4/graph.php')
          .then((response) => response.json())
          .then((responseJson) => {

            this.setState({
              isLoading: false,
              dataSource: responseJson,
            }, function(){

            });

          })
          .catch((error) =>{
            console.error(error);
          });
      }
    //here I return a static data ////////////////////////////
              render() {
                  const fill = 'rgb(134, 65, 244)'
                  const data = [50, 10, 40, 95, -4, -24, null, 85, undefined, 0, 35, 53, -53, 24, 50, -20, -80]

                  return (
                      <BarChart style={{ height: 200 }} data={data} svg={{ fill }} contentInset={{ top: 30, bottom: 30 }}>
                          <Grid />
                      </BarChart>
                  )
              }
          }

Я использую PHP в качестве серверной части для извлечения данных:

<?php

$serverName="DESKTOP-T5SLVUB\SQL2008R2";
$connectionInfo=array("Database"=>"Netos_DB","UID"=>"sa","PWD"=>"123");
$conn=sqlsrv_connect($serverName,$connectionInfo);   
$sql = "select ChiffreAffaire from V502_client where Mois=2 and Annee=2020 "; 
$stmt = sqlsrv_query( $conn, $sql  );
  while( $row[] = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
      $Item = $row;
      $json = json_encode($Item);}
    echo $json;

?>

Результат этого PHP файла:

[{"ChiffreAffaire":"4800.00"},{"ChiffreAffaire":"12000.00"},{"ChiffreAffaire":"1200.00"},{"ChiffreAffaire":"10032.00"}]

Как заменить данные (stati c data) с MySQL данными, которые я получаю в моем файле PHP?

...