How to generate a random values between two hexcode in flutter - flutter

I am trying to generate a list of hexcode between two values so I can use then to generate a range of colors.
What I am trying to achieve is this.
List.generate(36, (i) => generateRandomCode(0xFF0587D8, 0xFF0345B5))
generateRandomCode(min, max) {
// implementation here
}
How do I generate this int in the function generateRandomCode?

Generator:
int generateRandomCode(int minValue, int maxValue) {
return Random().nextInt((maxValue - minValue).abs() + 1) + min(minValue, maxValue);
}
Usage:
final list = List<int>.generate(36, (i) => generateRandomCode(0xFF0587D8, 0xFF0345B5));
print(list);
Result:
/flutter ( 6592): [4278422613, 4278508577, 4278489065, 4278486019, 4278499653, 4278480654, 4278464106, 4278474805, 4278462976, 4278549386, 4278537465, 4278418510, 4278496777, 4278405225, 4278411018, 4278412393, 4278461314, 4278538568, 4278549901, 4278510124, 4278492024, 4278530862, 4278517728, 4278425917, 4278442865, 4278497051, 4278430858, 4278497227, 4278462764, 4278412600, 4278448684, 4278422213, 4278464891, 4278473256, 4278543371, 4278476016]
You can still convert it to list of colors
final colorList = list.map((hex) => Color(hex)).toList();
print(colorList);
Result:
I/flutter ( 6592): [Color(0xff04c366), Color(0xff03d608), Color(0xff03a34a), Color(0xff048eac), Color(0xff03924a), Color(0xff03f0ba), Color(0xff052271), Color(0xff03ef8a), Color(0xff0582e0), Color(0xff0551ae), Color(0xff0402b3), Color(0xff0552be), Color(0xff050553), Color(0xff04c39f), Color(0xff053f88), Color(0xff04b6b8), Color(0xff05299f), Color(0xff03f1a7), Color(0xff03ca2f), Color(0xff04a864), Color(0xff04ee66), Color(0xff0358ce), Color(0xff03b741), Color(0xff046785), Color(0xff04ef11), Color(0xff04e618), Color(0xff03ff8a), Color(0xff03dc97), Color(0xff04353e), Color(0xff04cff6), Color(0xff03bfa4), Color(0xff049ca3), Color(0xff04bbac), Color(0xff03c5d3), Color(0xff05730b), Color(0xff036c8f)]

Use a List Generate Constructor
new List<int>.generate(3, (int index) => index * index);
Link to the source : Link generator

List<int> createList(int min, int max) {
return List.generate(max - min + 1, (i) => min + i);
}
Result:
print(">>>>>>>>>> ${createList(10, 22).join(", ")}");
// >>>>>>>>>> 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22

Related

Ionic BLE Manufacturer specific Data

Using #ionic-native/ble I'm able to scan and discover a BLE device which has manufacturer specific data.
According to the lib (https://github.com/don/cordova-plugin-ble-central#ios-1) here is the way to get this data
const mfgData = new Uint8Array(device.advertising.kCBAdvDataManufacturerData);
console.log('Manufacturer Data: ', mfgData);
const hex = Buffer.from(mfgData).toString('hex');
console.log(hex);
The encode to hex result being 2604 0504 386 55c0b
What I don't understand is the proper way to use this result to decode the manufacturer (company) id, which is supposed to be "0x0426"
You can try the following :
const toHexString = bytes =>
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
console.log(toHexString(new Uint8Array([0, 1, 2, 42, 100, 101, 102, 255])))

Drools comparing objects in working memory

I am trying to accumulate similar objects in working memory and set actions based on object attributes. My Rule: 3 or more procedures done on the same date for a patient by the same provider. Condition: Same date of service and same provider ID Action: 100% for 1st procedure, 50% for 2nd procedure and 25% for remaining. Each line item contains details about the procedure.
My Model object:
public class LineItem {
private Date dateOfService;
private String procedureCode;
private String providerID;
private double billedAmount;
private double allowableAmount;
//getters and setters
}
Drools Rule:
**rule "Multiple Procedures done on the same day by Same Provider"**
lock-on-active true
when
$lineItem1 : LineLevelData ( $dateOfService : dateOfService , $providerId : providerId, reasonCode == null, $lineNumber : lineNumber )
and
$lineItem2 : LineLevelData ( lineNumber!= $lineNumber , dateOfService == $dateOfService , providerId == $providerId, reasonCode == null )
and
accumulate( $lineItem: LineLevelData( dateOfService == $dateOfService , providerId == $providerId, reasonCode == null );
$list: collectList( $lineItem ) )
then
System.out.println("List size: " + $list.size () );
for ( int i = 0; i < $list.size(); i++ ){
LineLevelData lineItem = (LineLevelData)$list.get(i);
if(i == 0){
modify(lineItem){ setReimbursementAmount(0.8* ( lineItem.getBilledAmount() ) )
};
System.out.println("Line Number: " + lineItem.getLineNumber() );
}
else if(i == 1){
modify(lineItem) { setReimbursementAmount(0.5* (lineItem.getBilledAmount() ) )
};
System.out.println("Line Number: " + lineItem.getLineNumber() );
}
else {
modify(lineItem ){ setReimbursementAmount(0.25* (lineItem.getBilledAmount() ) )
};
System.out.println("Line Number: " + lineItem.getLineNumber() );
}
}
Java code:
LineLevelData line1Item = new LineLevelData();
line1Item.setLineNumber(1);
line1Item.setProcedureCode("99201");
line1Item.setDateOfService(new GregorianCalendar(2017, 9, 15).getTime());
line1Item.setBilledAmount(1000);
line1Item.setProviderId("670112");
billLineItems.add(line1Item);
LineLevelData line2Item = new LineLevelData();
line2Item.setLineNumber(2);
line2Item.setProcedureCode("99205");
**line2Item.setDateOfService(new GregorianCalendar(2017, 8, 20).getTime());
line2Item.setProviderId("670118");**
line2Item.setBilledAmount(1500);
billLineItems.add(line2Item);
LineLevelData line3Item = new LineLevelData();
line3Item.setLineNumber(3);
line3Item.setProcedureCode("99049");
**line3Item.setDateOfService(new GregorianCalendar(2017, 8, 20).getTime());
line3Item.setProviderId("670118");**
line3Item.setBilledAmount(1000);
billLineItems.add(line3Item);
LineLevelData line4Item = new LineLevelData();
line4Item.setLineNumber(4);
line4Item.setProcedureCode("99058");
**line4Item.setDateOfService(new GregorianCalendar(2017, 8, 20).getTime());**
line4Item.setBilledAmount(520);
**line4Item.setProviderId("670118");**
billLineItems.add(line4Item);
//Inserting facts in working memory
for(LineLevelData billLineItem : buildBillLineItems()){
kSession.insert(billLineItem);
log.info("Inserted Line Item : " + billLineItem.getLineNumber());
}
int rulesFired = kSession.fireAllRules(new RuleNameEqualsAgendaFilter("Multiple Procedures done on the same day by Same Provider"));
According to my test above line items with line number 2 , 3 and 4 should be updated. Instead I am getting line number 1, 3 and 4.
Rule Output:
List size: 3
Line Number: 4
Line Number: 3
Line Number: 1
Multiple Procedures rule is fired...Wed Sep 20 00:00:00 CDT 2017
===========Rule Fired============ : MULTIPLE PROCEDURES DONE ON THE SAME DAY BY SAME PROVIDER
No. of Rules fired: 1
Line Item Number: 1
Billed amount: 1500.0
Reimbursable amount: 375.0
Reason Code: CHI
Message: Coverage for all subsequent procedures is 25%
Line Item Number: 4
Billed amount: 520.0
Reimbursable amount: 416.0
Reason Code: NR
Message: Submit to Nurse Review. 100% of UCR at 80th percentile.
Line Item Number: 3
Billed amount: 1000.0
Reimbursable amount: 500.0
Reason Code: PHY
Message: Manual review is required. 50% coverage for secondary procedure.
**Line Item Number: 2
Billed amount: 1000.0
Reimbursable amount: 0.0
Reason Code: null
Message: null**

leafletR map doesn't load in shiny on start

I have the following little piece of code (more less as described HERE) - I want to control the number of points to be shown by a slider in shiny. You can see that the initial map is loaded after a little while (watch console output), but it will only show up after you used the slider once.
But I'd like the map to show up after it is created during launch of the shiny app - any hints how to do that?
## app.R ##
library(shiny)
library(shinydashboard)
library(httr)
library(leafletR)
data(quakes)
# dest_dir=tempdir()
dest_dir="foo_map"
dest_file = paste(dest_dir,"quakes","quakes.html",sep="\\")
dat = quakes
createMapHTML <- function(inputFreq=1) {
q.dat <- toGeoJSON(data=dat[seq(from = 1, to = nrow(dat), by=inputFreq), ],
dest=dest_dir, name="quakes")
sty <- styleSingle(col="darkblue", fill="darkblue", rad=6)
# create map
q.map <- leaflet(data=q.dat, dest=dest_dir, size = c(1200, 600), incl.data=TRUE,
base.map=list("osm"), style=sty, popup="*", controls="all")
}
# createMapHTML()
runApp(list(
ui = dashboardPage(
dashboardHeader(title = "quakes"),
dashboardSidebar(
sliderInput("slider", "#observations frequency:", 1, 100, 1)
),
dashboardBody(
htmlOutput("inc")
)
),
server = function(input, output, session) {
createMap <- reactive({
createMapHTML(input$slider)
return(includeHTML(dest_file))
})
output$inc<-renderUI({ createMap() })
}
))
so the bottleneck with the leafletR package is the conversion to GeoJson. Additionally the "includeHTML & htmlOutput" workaround for embedding the html out is flaky..
To avoid both I just switched to the leaflet packackage:
## app.R ##
library(shiny)
library(shinydashboard)
library(leaflet)
data(quakes)
dat = quakes
runApp(list(
ui = dashboardPage(
dashboardHeader(title = "quakes"),
dashboardSidebar(
sliderInput("slider", "#observations frequency:", 1, 100, 1)
),
dashboardBody(
leafletOutput("map", height = 600)
)
),
server = function(input, output) {
output$map <- renderLeaflet({
map <- leaflet() %>% addTiles()
map %>% addCircles(data=dat[seq(from = 1, to = nrow(dat), by=input$slider), ], #input$slider
lat = ~lat, lng = ~long, fillOpacity = 1.0)
})
}
))

CFileDialog constructor can't spot TRUE and fopen(str,"r") error

I call the CFileDialog constructor in the following method:
CFileDialog FileDlg(TRUE, ".txt", NULL, 0, strFilter);
according to the parameters in the CFileDialog constructor, the first parameter should be BOOL, but my compiler thinks it's int instead of BOOL. Can you tell me why?
Besides, in the code, when I use fopen(str,"r"), the error is no conversion function from CSring to const char*. Appreciate your discussion.
The code is:
void OnFileOpen()
{
CClientDC dc(this);
CString str;
CRect rc;
FILE *ifp;
char strFilter[] = { "TXT Files (*.txt)|*.txt|All Files(*.*)|*.*||" };
CFileDialog FileDlg(TRUE, ".txt", NULL, 0, strFilter);
if (FileDlg.DoModal() == IDOK)
{
str = FileDlg.GetFileName();
ifp = fopen(str,"r");
dc.TextOutW(350, 50, "File Opened: " + str);
for (int i = 1; i < n; i++)
{
fscanf(ifp, "%d %d", &pt[i].x, &pt[i].y);
rc = CRect(pt[i].x - 30, pt[i].y - 30, pt[i].x + 30, pt[i].y + 30);
dc.Ellipse(rc);
rc = CRect(pt[i].x - 1, pt[i].y - 1, pt[i].x + 1, pt[i].y + 1);
dc.Rectangle(rc);
}
fclose(ifp);
}
}

D3 Multi Line Graph with Dots

I am new to D3.js. I love it but I am having real trouble figuring out the best approach to structuring data.
I would ideally like to create a simple multiline graph that has over points over the selected points. Firstly I have the multiple lines created but trying to add the points has stumped me, and I think it has to do with the structure of my data.
Here is my working fiddle. I'm not sure if I should be trying to use d3.nest to re-arrange the data
I have a json object that I am retrieving from a google form which is all nice and smooth. This is what it looks like:
var data = [{
"description": "Global warming is a serious and pressing problem. We should begin taking steps now even if this involves significant costs",
"year2013": 40,
"year2012": 36,
"year2011": 41,
"year2010": 46,
"year2009": 48,
"year2008": 60,
"year2006": 68,
}, {
"description": "The problem of global warming should be addressed, but its effects will be gradual, so we can deal with the problem gradually by taking steps that are low in cost",
"year2013": 44,
"year2012": 45,
"year2011": 40,
"year2010": 40,
"year2009": 39,
"year2008": 32,
"year2006": 24,
}, {
"description": "Until we are sure that global warming is really a problem, we should not take any steps that would have economic costs",
"year2013": 16,
"year2012": 18,
"year2011": 19,
"year2010": 13,
"year2009": 13,
"year2008": 8,
"year2006": 7,
}, {
"description": "Don't know / refused",
"year2013": 1,
"year2012": 1,
"year2011": 1,
"year2010": 1,
"year2009": 1,
"year2008": 0,
"year2006": 1,
}]
Any help would be appreciated, I have been at it for days.
Cheers!
First - I would flatten your data
data = [
{date:"2011",type: "line0", amount:20}
...
]
Then nest your data by type
nested = d3.nest()
.key( (d) -> return d.type )
.entries(data)
Then append your line groups
# Line Groups
groups = container.selectAll('g.full-line')
.data(nested, (d) -> return d.key )
# ENTER
groups.enter().append('svg:g')
.attr( 'class', (d,i) -> "full-line#{i}" )
# EXIT
d3.transition(groups.exit()).remove()
# TRANSITION
d3.transition(groups)
Then append your chart lines
# Individual Lines
lines = groups.selectAll('.line').data (d)-> [d.values]
# ENTER
lines.enter().append("svg:path")
.attr("class","line")
.attr("d", d3.svg.line()
.interpolate(interpolate)
.defined(defined)
.x( (d,i) -> return xScale(d,i) )
.y( (d,i) -> return yScale(d,i) ) )
# EXIT
d3.transition( groups.exit().selectAll('.line') )
.attr("d",
d3.svg.line()
.interpolate(interpolate)
.defined(defined)
.x( (d,i) -> return xScale(d,i) )
.y( (d,i) -> return yScale(d,i) ) )
# TRANSITION
d3.transition(lines)
.attr("d",
d3.svg.line()
.interpolate(interpolate)
.defined(defined)
.x( (d,i) -> return xScale(d,i) )
.y( (d,i) -> return yScale(d,i) ) )
Thanks
I ended up using something similar.
/* Transform Data */
data = data.map(function (d) {
return {
country: d.country,
date: new Date(d.year.toString()),
value: d.value
};
});
/* Nest Data */
data = d3.nest().key(function (d) {
return d.country;
}).entries(data);`